Backbone - 在多个事件后渲染视图
Backbone - Render view after multiple events
我有一个 Backbone View
,我只想在两个不同的事件发生后才渲染它。这两个事件是从服务器填充的两个不同 Collections
(这是填充两个下拉 select 列表)。
我的两个 Collections
是 cFilterFields
和 cFilterOperators
,它们都有一个自定义 .populate()
方法调用 .fetch()
并触发 populated
事件.
这是我的:
var MyView = Backbone.View.extend({
tagName: 'div',
initialize: function(){
_.bindAll(this,'render');
this.fields = new cFilterFields();
this.operators = new cFilterOperators();
this.fields.populate();
this.operators.populate();
this.listenTo(this.fields,'populated',function(){
// ...
});
this.listenTo(this.operators,'populated',function(){
// ...
});
},
events: {},
render: function(){
// ...
return this;
}
});
当 两个 Collection
事件都已触发时呈现此 View
的最佳方式是什么...?
使用集合的 populate
方法 return 承诺,然后使用 jQuery.when
你的初始化应该看起来像这样
$.when(fields.populate(), operators.populate())
.done(_.bind(function() {
this.render();
}, this));
此解决方案假定 JQuery 和下划线,因为它们都是 Backbone 的依赖项。
刚刚完成,真的很简单,你只需要做:
this.listenTo(this.fields && this.operators,'populated',function(){
this.render();
});
更新
不要使用这个,看下面的评论,我会接受正确的答案。
我有一个 Backbone View
,我只想在两个不同的事件发生后才渲染它。这两个事件是从服务器填充的两个不同 Collections
(这是填充两个下拉 select 列表)。
我的两个 Collections
是 cFilterFields
和 cFilterOperators
,它们都有一个自定义 .populate()
方法调用 .fetch()
并触发 populated
事件.
这是我的:
var MyView = Backbone.View.extend({
tagName: 'div',
initialize: function(){
_.bindAll(this,'render');
this.fields = new cFilterFields();
this.operators = new cFilterOperators();
this.fields.populate();
this.operators.populate();
this.listenTo(this.fields,'populated',function(){
// ...
});
this.listenTo(this.operators,'populated',function(){
// ...
});
},
events: {},
render: function(){
// ...
return this;
}
});
当 两个 Collection
事件都已触发时呈现此 View
的最佳方式是什么...?
使用集合的 populate
方法 return 承诺,然后使用 jQuery.when
你的初始化应该看起来像这样
$.when(fields.populate(), operators.populate())
.done(_.bind(function() {
this.render();
}, this));
此解决方案假定 JQuery 和下划线,因为它们都是 Backbone 的依赖项。
刚刚完成,真的很简单,你只需要做:
this.listenTo(this.fields && this.operators,'populated',function(){
this.render();
});
更新
不要使用这个,看下面的评论,我会接受正确的答案。