Saturday, 17 August 2013

Waiting for multiple Ajax calls

Waiting for multiple Ajax calls

I have an each-loop, below in lines 1-14, that goes through div#canvas
(nothing to do with HTML5's canvas) looking for elements that have to be
loaded. Basically, if an element has a target attribute (line 1) and the
target pointed to (target$) is not in div#canvas (line 5) an Ajax $.post
request is launched (line 6) to bring the missing target in from the
server and prepend it to div#canvas.
$("#canvas .active[target]").each(function() {
this$ = $(this);
targetName = this$.attr('target');
target$ = $('div[name=' + targetName + ']');
if (target$.length == 0) {
$.post('loadData.php', { fileName : targetName + '.xml'
},function(xml) {
canvasData$ = $(xml).find("canvasData");
$('#canvas').prepend(canvasData$);
});
}
});
When all of these missing targets have been loaded I want to issue an Ajax
request to send all of div#canvas to the server. That call looks like
this:
Status$.load('writePage.php', {
pageName: pageName,
seg1: seg1,
canvas: canvasOuterHTML,
seg2: seg2
});
Could someone suggest the best way to hold off the final call to writePage
until all of the missing target reads have completed?
Thanks.

No comments:

Post a Comment