| Page 1 of 1 | [ 8 posts ] |
Inactivist
|
I'm using the YUI3 Autocomplete widget with a URL source returning JSON data.
I'm looking for a simple way to detect HTTP error responses, such as when the user is not authenticated. (401 response from server.) Example: Code: Y.one('#ac-input').plug(Y.Plugin.AutoComplete, { resultHighlighter : 'phraseMatch', source : "http://example.com/api/item/search/?q={query}", }); If the user hasn't authenticated on the server, the Autocomplete widget receives a 401 response and fails silently but logs the error to the browser's JavaScript console. I'd like to detect any error response so I can show an appropriate status or otherwise handle the error. I've searched but haven't found documentation specific to this issue - source diving didn't help, either. I *think* I'll have to roll my own datasource object so I can issue the request and detect the error. If that's the case, is there an online example for this method? Or is there a simpler way to pass an error event handler for this type of data source? |
|
I'm not an expert in AutoComplete, but you "should" be able to add a "resultListLocator" to check the results as they come in and verify that the user needs to be warned.
http://yuilibrary.com/yui/docs/autocomp ... tocomplete |
Inactivist
|
@davglass: Thanks, I'll check out that suggestion.
|
Inactivist
|
@davglass: I tried the callable resultListLocator suggestion, but when a 401 error is returned by the server, the resultListLocator function is never invoked (I verified that it is called when the user is authenticated.)
Here's my test resultListLocator: Code: function myLocator(response) { console.log(arguments); return response.objects; }; This message appears in the Chrome console log: Code: GET http://example.com/api/item/search/?q=test 401 (UNAUTHORIZED) Along with a bunch of YUI3 diagnostic traces (file/stack info). I'm using YUI 3.4.1. |
Inactivist
|
I'm hoping it's a simple as supplying an
Code: on: { failure: myHandler } parameter to the Autocomplete widget. But that seems not to be the case. |
Inactivist
|
Someone provided a working solution on StackOverflow:
Code: var ds = new Y.DataSource.IO({ source: 'http://example.com/api/item/search/' }); ds.set('ioConfig', { on: { failure: function (transactionID, ioResponseObj, args) { ... } } }; Y.one('#ac-input').plug(Y.Plugin.AutoComplete, { requestTemplate: '?q={query}', source: ds }); http://stackoverflow.com/a/10023472/1309774 |
Inactivist
|
Another way to do this without resorting to the use of the IO object adapter: use a global IO failure event handler.
While I would rather handle the error locally in the AutoComplete widget handlers, alas, it's not that simple. Using Y.on('io:failure') event handling may be a much more direct approach if all you need to do is display info about comms errors. Code: // // Handle IO failure event function ioFailureFunction(transaction, xhrObject) { console.log("IO:FAILURE", arguments); errCode = xhrObject.status; // Insert the current error status into an HTML element... Y.one('#error').setContent(errCode); }; Y.on('io:failure', ioFailureFunction); |
Inactivist
|
I'm still trying to figure out why I can't attach a local handler to the AutoComplete widget's 'on' map. It should be just that simple, shouldn't it?
|
| Page 1 of 1 | [ 8 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group