Backbone.js 从 collection 中删除模型
Backbone.js remove model from collection
我正在学习 Backbone,我无法从 collection 中删除模型。 collection 'remove' 事件触发,但 collection 似乎保持不变。这是我目前所拥有的:http://jsbin.com/becamo/edit?js,output
我在点击后从 collection 中删除了模型。
然后,列表视图正在侦听 collection 移除事件并再次调用 render()。
我可以从 console.log() 中看到删除事件已触发,但没有任何变化。当我检查 collection 变量时,它没有改变。我在互联网上搜索线索时尝试了大约 50 种变体,但似乎没有任何效果。
您不需要在集合上实施 remove
- 只需删除覆盖默认设置的尝试即可解决问题。所以集合实现变成:
var UserCollection = Backbone.Collection.extend({
model: User
});
而不是:
var UserCollection = Backbone.Collection.extend({
model: User,
initialize: function() {
this.on('remove', this.remove);
},
remove: function() {
console.log('Collection Event: REMOVE');
}
});
我正在学习 Backbone,我无法从 collection 中删除模型。 collection 'remove' 事件触发,但 collection 似乎保持不变。这是我目前所拥有的:http://jsbin.com/becamo/edit?js,output
我在点击后从 collection 中删除了模型。
然后,列表视图正在侦听 collection 移除事件并再次调用 render()。
我可以从 console.log() 中看到删除事件已触发,但没有任何变化。当我检查 collection 变量时,它没有改变。我在互联网上搜索线索时尝试了大约 50 种变体,但似乎没有任何效果。
您不需要在集合上实施 remove
- 只需删除覆盖默认设置的尝试即可解决问题。所以集合实现变成:
var UserCollection = Backbone.Collection.extend({
model: User
});
而不是:
var UserCollection = Backbone.Collection.extend({
model: User,
initialize: function() {
this.on('remove', this.remove);
},
remove: function() {
console.log('Collection Event: REMOVE');
}
});