[英]How Can I Reset This Backbone View on Collection Change?
如何將此視圖綁定到集合更改事件,以便在將新項目添加到集合時重置?
KAC.Views.ModuleMainNavigation = Backbone.View.extend(
{
tagName: "div",
id: "",
className: "",
template: JST['modules/main_navigation'],
initialize: function() {
_.bindAll(this);
},
events: {
},
render: function () {
this.$el.html(
this.template(
{
collection : this.collection
}
)
);
return this;
}
}
);
你必須聽取改變事件。
大多數情況下,這是在initialize
函數中完成的。
您可以監聽所有事件(模型更改,集合重置,新模型,模型已刪除):
this.collection.on('change reset add remove', this.render, this);
或僅適用於新模型添加事件:
this.collection.on('add', this.render, this);
另請參見backbone.js集合事件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.