I guess this question isn't only specific to YUI, but that's the JS library I'm using and a specific answer would be helpful.
Basically when loading a page, I want a script to run a few XHRs using Y.io, and if they all return the data successfully then I want the script to move on to the next step, which will manipulate the data received.
I can think of a few ways of doing this, but they all seem a bit clumsy to me and I hope someone has a better suggestion. My ideas so far:
Any better ideas out there? I'm not really liking either of mine at the moment, but I'm leaning towards option two.
You shouldn't need to queue the Y.io calls. Just send them all at once and when they all return, move on.
var data = {};
function step(id, o, part) {
try {
data[part] = Y.Lang.JSON.parse(o.responseText);
if (data.a && data.b && data.c) {
processData(data);
}
}
catch (e) {
/* handle bad JSON, failure case */
}
}
Y.io(a_url, { on: { success: step }, 'arguments': 'a' });
Y.io(b_url, { on: { success: step }, 'arguments': 'b' });
Y.io(c_url, { on: { success: step }, 'arguments': 'c' });