0
我創建了一個視圖,它正在收聽一個集合。這個集合的模型被一次全部替換。我希望儘可能少地呈現視圖。Backbone:我應該聽什麼事件
查看:
BoxContent = Backbone.View.extend({
initialize: function(options) {
console.log("BoxContent initializing");
this.el = options.el;
this.collection = options.collection;
this.collection.on("add", this.update, this);
this.collection.on("reset", this.update, this);
},
update: function(){
this.render();
},
render: function() {
document.getElementById('boxContentHeader').innerHTML = localStorage.activeBox;
console.log("BoxContent rendering");
var temp = _.template(maincontemp,{boxFolder: this.collection});
this.$el.empty();
this.$el.append(temp);
this.$el.trigger("create");
},
reset: function()
{
this.render();
},
close: function(){
//console.log("off-logging clickListener");
//this.collection.off();
//$(this.el).off();
}
});
要更新我創建模型的一個數組集合,重新集合,並把新的數組集合。我需要傾聽重置,以使空集合呈現。 我不從平安服務器獲取數據。
是否有其他方法來收聽集合的更改?
編輯:
一個問題:雖然我只是添加的車型之一陣列收集,是骨幹呼籲每個模型的add事件這個數組裏面?
是常見的車型陣列添加到收藏? 或按模型添加模型會更好嗎? – marcel
兩者都很好,這是來自'collection.reset'的官方文檔:*每次添加和刪除一個模型都很好,但是有時候您有太多的模型需要更改,您寧願更新收集散裝* – msvalkon
謝謝,所以我繼續我的陣列。 – marcel