javascript 删除集合中的模型并触发删除事件-backbone.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15068036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove model in collection and fire remove event - backbone.js
提问by Hello-World
how do I remove remove a model in collection and make the remove event fire . I tried people.remove([{ name: "joe3" }]);
but it wont work.
如何移除移除集合中的模型并使移除事件触发。我试过了, people.remove([{ name: "joe3" }]);
但它不起作用。
var Person = Backbone.Model.extend({
initialize: function () {
console.log(" person is initialized");
},
defaults: {
name: "underfined",
age:"underfined"
}
});
var People = Backbone.Collection.extend({
initialize: function () {
console.log("people collection is initialized");
this.bind('add', this.onModelAdded, this);
this.bind('remove', this.onModelRemoved, this);
},
model: Person,
onModelAdded: function(model, collection, options) {
console.log("options = ", options);
alert("added");
},
onModelRemoved: function (model, collection, options) {
console.log("options = ", options);
alert("removed");
},
});
//var person = new Person({ name: "joe1" });
var people = new People();
//people.add([{ name: "joe2" }]);
people.add([{ name: "joe1" }]);
people.add([{ name: "joe2" }]);
people.add([{ name: "joe3" }]);
people.add([{ name: "joe4" }]);
people.add([{ name: "joe5" }]);
people.remove([{ name: "joe3" }]);
console.log(people.toJSON());
回答by Benjamin Kaiser
For anyone else looking for a remove where, you can simply chain it with a collection.where call. like so to remove all items matching the search:
对于正在寻找删除位置的其他人,您可以简单地将它与 collection.where 调用链接起来。像这样删除所有与搜索匹配的项目:
people.remove(people.where({name: "joe3"}));
回答by dfsq
By doing:
通过做:
people.remove([{ name: "joe3" }]);
you don't remove a model, because you pass just an plain object which is not connected to people
collection. Instead you could do something like this:
您不会删除模型,因为您只传递了一个未连接到people
集合的普通对象。相反,您可以执行以下操作:
people.remove(people.at(2));
Or:
或者:
var model = new Person({name: "joe3"});
people.add(model);
...
people.remove(model);
will work as well.
也会起作用。
So you need to reference actual model object from a collection;
所以你需要从集合中引用实际的模型对象;
回答by Igor Shubin
var Person = Backbone.Model.extend({
defaults: {
name: "underfined",
age:"underfined"
}
});
var People = Backbone.Collection.extend({
initialize: function () {
this.bind('remove', this.onModelRemoved, this);
},
model: Person,
onModelRemoved: function (model, collection, options) {
alert("removed");
},
getByName: function(name){
return this.filter(function(val) {
return val.get("name") === name;
})
}
});
var people = new People();
people.add(new Person({name:"joe1"}));
people.add(new Person({name:"joe2"}));
people.remove(people.getByName("joe1"));
console.info(people.toJSON());
回答by starikovs
Another way is shorter a little and fires the removeevent for a collection as well:
另一种方法更短一点,并且也会为集合触发remove事件:
people.at(2).destroy();
// OR
people.where({name: "joe2"})[0].destroy();
Triggers a "destroy" event on the model, which will bubble up through any collections that contain it.. http://backbonejs.org/#Model-destroy
在模型上触发“销毁”事件,该事件将通过包含它的任何集合冒泡.. http://backbonejs.org/#Model-destroy
回答by Sasha Bichkov
In order to remove "[0]" you can use the following code:
为了删除“[0]”,您可以使用以下代码:
people.findWhere({name: "joe2"}).destroy();