1

I been reading a little about the jquery when and done functionality, but what is the best way to handle an arbitrary number of requests:

var t = [];

_.each(searchData.get("products"), function(productId, index){
    fetchingProduct = App.request("product:entity", productId);
    $.when(fetchingProduct).done(function(product){
        t.push(product);
    });
});

console.log(t);

This does obviously not work, but how would I construct something simlilar that will?

Thanks

1 Answers1

0

Assuming that your App.request() method returns a jQuery promise, you can create an array of them to apply to $.when:

var t = [], requests = [];
_.each(searchData.get("products"), function(productId, index){
    requests.push(App.request("product:entity", productId).done(function(product) {
        t.push(product);
    });
});

$.when.apply($, requests).done(function() {
    console.log(t);
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339