Here's the scenario. I'm writing a routine that will take in a list of "items". Each item will fire an Ajax request and the resulting data is placed into a variable identified in the "items" properties.

Looping over the items to do the Ajax call results in the LAST item's variable being updated. This makes sense in that the callback happens after the loop has completed.

So the question is how do I have the callback set the correct variable?

Here's some sample code if it helps:

//loop over each item and set the data as needed.
for (var x = 0; x < options.items.length; x++ ) {
  var i = options.items[x];
  if (!i.ajaxparams) { return; };
var macb = function (data) { queueData[i.dataname] = data; checkComplete(); }
    i.ajaxparams.success = macb;
    $.ajax(i.ajaxparams);
  }
}

The problem is the "macb" function - specifically the use of the i.dataname. This works great if I have ONE ajax item. But adding two (say for "names" and "scores") only sets the last dataname seen in the loop.

doing the "var i = " *should* have prevented this, but isn't. (and my brain is mush right now). So the next option is to pass in the dataname value to the callback function. Except I don't see any possible way to pass extra data to the callback.

Is there a better way to handle this? I'm afraid the "obvious" answers probably aren't suitable in this case. Unless my brain is worse off than I thought right now.

Thanks for any tips.

Shawn

ps. I snipped a little bit from the above code. There's logic to check if a "success" function is specified in the ajaxparams and if so use it's return value to set the queueData property.

Reply via email to