最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how can i sync two Backbone fetch calls with JQuery WHEN and THEN - Stack Overflow

programmeradmin1浏览0评论

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. ;-)

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. ;-)

Share Improve this question asked Jun 4, 2012 at 15:09 GuyGuy 13.4k17 gold badges89 silver badges129 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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.

发布评论

评论列表(0)

  1. 暂无评论