gpt4 book ai didi

jquery - Backbone,当全部加载完毕时触发事件

转载 作者:行者123 更新时间:2023-12-01 02:35:26 24 4
gpt4 key购买 nike

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

我将使用 jquery 延迟方法($.when、$.then)同时打印所有内容,如果我在 View 上使用“绑定(bind)事件”,该怎么做?

这是代码:

路由器:

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

关于jquery - Backbone,当全部加载完毕时触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7150533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com