i found out a cool thing in jquery: you can do:
$.when([$.get(..), $.ajax(...), $.getJSON(...)]).then(function(data1,data2,data3){
// code to run when all requests are done
});
this is great when you want to sync many ajax calls.
in backbone an ajax call is being issued every time you fetch a model or collection:
cardsCollection.fetch();
my question is how can i achieve similar syncing capabilities with backbone model/collection fetching:
i would like to do something like:
$.when([series.fetch(), cardsCollection.fetch()]).then(function(){
cardsListView.seriesID = seriesID;
cardsListView.seriesName = series.get('seriesName');
cardsListView.template = _.template(CardsListTemplate);
cardsListView.render();
$('#loader').hide();
});
is it possible?
tnx. ;-)
Yes, it's possible. Just pass several Deferred to jQuery.when
: $.when(series.fetch(), cardsCollection.fetch()).then(...)
$.when
does not accept an array, you would need to use .apply
with $.when
.
$.when.apply($,[series.fetch(), cardsCollection.fetch()]).done(dostuff);
However, series.fetch()
and cardsCollection.fetch()
would have to return deferred objects.
If you don't use .apply
, it will resolve instantly because you didn't give it a deferred object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With