简体   繁体   English

Backbone.js从集合中删除模型

[英]Backbone.js remove model from collection

I'm learning Backbone and for the life of me I can't remove a model from a collection. 我正在学习Backbone,对于我自己的一生,我无法从收藏夹中删除模型。 The collection 'remove' event fires, but the collection appears to remain the same. 会触发集合“删除”事件,但集合似乎保持不变。 Here's what I have so far: http://jsbin.com/becamo/edit?js,output 这是我到目前为止的内容: http : //jsbin.com/becamo/edit?js,output

I remove the model from the collection upon click. 单击后,我将从集合中删除该模型。

Then, the list view is listening for the collection remove event and calls render() again. 然后,列表视图正在侦听collection remove事件并再次调用render()。

I can see from a console.log() that the remove event fires, but nothing changes. 从console.log()可以看到,触发了remove事件,但是没有任何变化。 And when I inspect the collection variable it's unchanged. 当我检查collection变量时,它没有变化。 I've tried probably 50 variations now whilst searching the internet for clues and nothing seems to work. 我现在已经尝试了50种变体,同时在互联网上寻找线索,但似乎没有任何效果。

You do not need to implement remove on the collection -- simply removing your attempt to override the default fixes the issue. 您无需在集合上实施remove ,只需删除尝试覆盖默认值的尝试即可解决此问题。 So the collection implementation becomes: 因此,收集实现变为:

var UserCollection = Backbone.Collection.extend({
  model: User
});

Instead of: 代替:

var UserCollection = Backbone.Collection.extend({
  model: User,
  initialize: function() {
    this.on('remove', this.remove);
  },
  remove: function() {
    console.log('Collection Event: REMOVE');
  }
});

http://jsbin.com/jefudiyido/1/edit?js,output http://jsbin.com/jefudiyido/1/edit?js,输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM