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

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

我会使用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? // ????????????????????????? }); } }); 

查看1:

 App.Views.view_1 = Backbone.View.extend({ initialize: function() { _.bindAll(this, 'render'); this.model.bind('reset', this.render); }, render: function() { // print the data... } }); 

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

不确定您的延期活动在哪里。 Backbone已经有办法做到这一点。

在您的视图中,将事件“refresh”从集合绑定到视图渲染function。 每当您在集合上调用fetch时,都会触发刷新事件并重新呈现您的视图。

或许下划线是您正在寻找的方法?

延迟调用函数直到当前调用堆栈已清除,类似于使用延迟为0的setTimeout。用于在不阻止UI线程更新的情况下以块的forms执行昂贵的计算或HTML呈现。