How to synchronize multiple Backbone.js fetches?

前端 未结 3 2169
抹茶落季
抹茶落季 2020-12-23 21:28

I’m a bit new to Backbone.js, but am already impressed by everything it can do for me, and am trying to learn the patterns and best practices now.

I have two collect

3条回答
  •  星月不相逢
    2020-12-23 22:26

    As @JayC said you could use $.when from jQuery, other option that I prefer is the after function (from Underscore), which execute one time just after expected calls are completed.

    http://underscorejs.org/#after

    function runMyApp(){
      // run the needed code here.
      console.log('This will run one time but until both fetch are being completed.');
    }
    
    //2 because you have two collections
    var renderApp = _.after(2, runMyApp);
    collA.fetch({success: renderApp});
    collB.fetch({success: renderApp});
    

提交回复
热议问题