简体   繁体   English

删除集合中的模型并触发删除事件 - backbone.js

[英]remove model in collection and fire remove event - backbone.js

how do I remove remove a model in collection and make the remove event fire . 如何删除集合中的模型并使remove事件触发。 I tried people.remove([{ name: "joe3" }]); 我试过people.remove([{ name: "joe3" }]); but it wont work. 但它不会工作。

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());

For anyone else looking for a remove where, you can simply chain it with a collection.where call. 对于其他寻找删除位置的人,您可以使用collection.where调用链接它。 like so to remove all items matching the search: 像这样删除与搜索匹配的所有项目:

people.remove(people.where({name: "joe3"}));

see Backbone collection.where 请参阅Backbone collection.where

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. 您不删除模型,因为您只传递一个未连接到people集合的普通对象。 Instead you could do something like this: 相反,你可以做这样的事情:

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; 所以你需要从集合中引用实际的模型对象;

http://jsfiddle.net/kD9Xu/ http://jsfiddle.net/kD9Xu/

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());

Another way is shorter a little and fires the remove event 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 触发模型上的“destroy”事件,该事件将通过包含它的任何集合冒泡.http://backbonejs.org/#Model-destroy

要删除“[0]”,您可以使用以下代码:

people.findWhere({name: "joe2"}).destroy();

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

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