我有一个看似简单的例子,但我无法弄清楚。我有最新版本的 Backbone,因此“添加”应该在获取时自动触发。我已验证其他方式的获取工作正常。
这里定义了事件监听器
var InstallerListView = Backbone.View.extend({
$el: $('#installers-list'),
initialize: function (args) {
_.extend(this, args);
this.installers.on('add', function (model) {
// not reached
console.log('add triggered');
});
this.installers.on('reset', function () {
// reached
console.log('reset triggered');
});
}
});
这是我的获取调用
installers.fetch({
reset: true,
data: {/* some data */}
});
任何帮助“添加”火力的帮助都是值得赞赏的。
最佳答案
当您将 Reset 传递到 fetch 调用主干时,会抑制所有 add
和 remove
事件,并在最后仅触发一个重置事件。
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 granular add or remove events. Fires reset when finished. Useful for bulk operations and optimizations.
如果有必要,您可以随时绑定(bind)到重置事件并自行在每个模型上触发添加事件,或者如果您需要在添加每个模型时触发它,您可以 override您的收藏的重置事件。
关于javascript - Backbone.Collection 触发 'reset' 但不是 'add',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24659784/