javascript - What's the best way to call a function in YUI3 only if a series o XHR (IO) requests are successful? -
I think this question is not specific to YUI only, but this is the JS library I am using and A specific answer may be helpful.
Actually when a page loads, I want to run some XHRs using a script using Y.io, and if they want to successfully return all data, I want to move to the next step, which will manipulate the data received.
I can think of some ways of doing this, but all of them seem strange to me and I hope someone has a better suggestion. My thoughts are so far:
- I consolidate all the data in a JSON reaction, so if that one request is good, then go ahead. (I like this solution at least)
- When the Y.io request is successful for the first time, make the next call, and so on, and when the last successful, I know that everything has been successful and is moving forward. The next step of the script.
Is there any better idea? I'm not really liking any of my time this time, but I'm tilting towards alternative two.
You do not have to queue Y.io calls just send them all at the same time and When they all come back, then go ahead.
var data = {}; Function Step (id, o, part) {try {data [part] = Y.Lang.JSON.parse (o.responseText); If (data .a & amp; data.b & amp; ddQ) {process data (data); }} Hold (e) {/ bad JSON, failure case}}};}; (a_url, {on: {success: step}, 'arguments': 'a'}); Y.io (b_url, {on: {success: phase}, 'logic': 'b'}); Y.io (c_url, {on: {success: phase}, 'logic': 'c'});
Comments
Post a Comment