Synchronous GET request with YUI 2.7?
-
03-07-2019 - |
Question
i'm relatively new to YUI - browsing their great docs, i do not find a method or a flag to load an external resource synchronously.
or the question the other way around; for each matched node i need to call a method, which inserts something at the node; with asynchronous calls the remembered identifiers seem to mess up.
therefore the callback need to stick to the
pid
when the function is called, not when the callback gets executed - am i getting this right?
var platform_ids = YAHOO.util.Selector.query('.platform_id');
for (var i = 0; i < platform_ids.length; i++) {
var pid = platform_ids[i].getAttribute("id");
var sUrl = "/platform/" + pid + "/description/";
var callback = { success: function(o) {
document.getElementById(pid).innerHTML = o.responseText; }}
var transaction = YAHOO.util.Connect.asyncRequest(
'GET', sUrl, callback, null
);
}
thanks. MYYN
Solution
You don't want a synchronous request. The user experience can be awful. You really just want to pass a value to your callback so that it's not relying on pid
(which, as you've noticed, will usually have a different value when your callback is called):
var callback = { success: function(o)
{
document.getElementById(o.argument).innerHTML = o.responseText;
},
argument: pid
};
Here, I use the argument
callback member to hold the ID, and reference that in the callback function itself, ensuring each callback uses the correct ID.