Backbone,在加载所有内容时触发事件

kee*_*web 2 jquery backbone.js jquery-deferred

在我的骨干应用程序中,我加载3个集合,并在渲染功能中绑定"重置"事件.因此,通过这种方式,当我获取集合时,我会打印各种结果,但不能同时打印.

我会使用jquery延迟方法($ .when,$ .then)同时打印所有内容,如果我在视图上使用"绑定事件",如何执行此操作?

这是代码:

路由器:

App.Routers.test1 = Backbone.Router.extend({  

    routes: {       
        "/test" : "test"        
    },

    initialize: function() {                            
        // Models
        this.Models_1 =     new App.Collections.coll_1;     
        this.Models_2 =     new App.Collections.coll_2;
        this.Models_3 =     new App.Collections.coll_3;

        // Views
        this.View_1 =       new App.Views.view_1( {model: this.Models_1} );
        this.View_2 =       new App.Views.view_2( {model: this.Models_2} );
        this.View_3 =       new App.Views.view_3( {model: this.Models_3} );         
    },

    test: function() { 
        $.when(

            this.Models_1.fetch(),
            this.Models_2.fetch(),
            this.Models_3.fetch()

        ).then(function() {

            // ?????????????????????????

            //  What should I do here?

            // ?????????????????????????

        });
    }

});
Run Code Online (Sandbox Code Playgroud)

查看1:

App.Views.view_1 = Backbone.View.extend({

    initialize: function() {
        _.bindAll(this, 'render');
        this.model.bind('reset', this.render);
    },

    render: function() {

        // print the data...

    }

}); 
Run Code Online (Sandbox Code Playgroud)

ant*_*_Ti 6

test: function() { 
    $.when(
        this.Models_1.fetch({silent: YES}), // silent will prevent triggering any events on reset or add
        this.Models_2.fetch({silent: YES}),
        this.Models_3.fetch({silent: YES})
    ).then(_.bind(function() {
        this.Models_1.trigger('reset'); // manually trigger events in sequence you want
        this.Models_3.trigger('reset');
        this.Models_2.trigger('reset');
    }, this));
}
Run Code Online (Sandbox Code Playgroud)