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

标签 javascript backbone.js

如何删除删除集合中的模型并使删除事件触发。我试过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());

最佳答案

对于其他寻找删除位置的人,您可以简单地使用 collection.where 调用将其链接起来。像这样删除与搜索匹配的所有项目:

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

Backbone collection.where

关于javascript - 删除集合中的模型并触发删除事件-backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15068036/

相关文章:

javascript - jquery:在运行时创建/读取 .css 样式

javascript - Browserify:IE8 兼容性

javascript - 在 DOM 中插入 View 元素

javascript - 渲染 Backbone.js View ,模型中未定义模板变量

javascript - ie8更新值事件

Javascript 样式问题

Javascript 在数组中搜索

data-binding - 究竟什么是绑定(bind)?

javascript - 如何在 Titanium 中解析本地 XML 文件?

css - 如何强制行内 block 元素换行?