我想使用collection.reset()並沒有得到我期待在UI中的結果。響應返回2個結果,如預期的那樣。當我檢查我的收藏時,它總是告訴我我有2個結果,這也是正確的。雙渲染視圖
但是,在我的html輸出中,它只是將2個新獲取的行附加到表中。
initialize: function() {
_.bindAll(this,'buildRows','refreshTable');
this.template = _.template(maintmpl);
this.collection = txnList;
this.collection.bind("all", this.buildRows, this);
this.collection.on("reset", this.refreshTable, this);
this.collection.fetch();
},
events: {
"click #btn" : "refreshTable"
},
render: function() {
this.$el.append(this.template());
},
refreshTable: function() {
this.collection.reset();
console.log(this.collection.length)
this.collection.fetch();
},
buildRows: function(){
var mainview = this;
_(this.collection.models).each(function(model){
mainview.appendRow(model);
});
},
appendRow: function(model,i) {
var row = txnRow(model);
$('tbody',this.el).append(row.render().el);
}
所以最初,我使這個:
ROW1
行2
但與觸發refreshTable只追加2個行表按鈕的每次點擊:
ROW1
排2
排1
排2
Row1
Row2
我在想什麼?
爲什麼要調用'collection.reset()'來響應集合中的一個''reset''事件? –
我一直在嘗試使用reset()和fetch()方法的各種不同方式。顯然這些事件以某種方式引發了對方,但我不確定他們發射的確切順序。無論如何,我的代碼已經變得有點混亂,我想我錯誤地複製/粘貼了它。我甚至沒有意識到這一點,我一直盯着這個代碼這麼久。 :) – gerald