我在主幹0.5.3上面臨同樣的問題。
看着Backbone.Collection.reset()實現(這是後一個叫取(),如果你不提供任何 「添加」 可選屬性),線503至511:
// When you have more items than you want to add or remove individually,
// you can reset the entire set with a new list of models, without firing
// any `added` or `removed` events. Fires `reset` when finished.
reset : function(models, options) {
models || (models = []);
options || (options = {});
this.each(this._removeReference);
this._reset();
this.add(models, {silent: true});
if (!options.silent) this.trigger('reset', this, options);
return this;
},
2這裏重要的事情:
this.add(models, {silent: true});
這意味着你將不會有任何「添加」事件觸發。
第二件事是:
if (!options.silent) this.trigger('reset', this, options);
這意味着,如果你替換代碼:
var c = new AwesomeCollection();
c.bind("reset", function(){
console.log('Collection has changed.');
}
c.add({testModel: "Test"}); // Shouldn't this trigger the above log statement?
它應該工作(爲我工作)
將綁定從「更改」更改爲「添加」仍然不會產生此。嗯......問題可能在其他地方。 – Thomas
不要忘記你可以綁定多個事件,例如:'c.bind(「add remove update」,function(){});' –