And*_*raD 23 backbone.js backbone-events
我有一个简单的问题.我正在寻找一个包含2行代码的函数:
deleteTask: function() {
this.parent.collection.remove(this.model);
this.model.destroy();
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉第一行,它应该从其集合中删除模型,那么事情似乎按预期工作(如同,模型被自动删除).从Backbone的网站上,这是模型"破坏"功能的相关描述:
触发模型上的"销毁"事件,该事件将通过包含它的任何集合冒泡.
我可以安全地假设删除this.parent.collection.remove(this.model);不会以任何方式影响代码的功能吗?这是我的想法,但我想确保它.
谢谢!
asg*_*eo1 35
如果销毁模型,则会从包含模型的任何集合中删除它.您可以在骨干源中看到
//Internal method called every time a model in the set fires an event.
_onModelEvent: function(event, model, collection, options) {
...
if (event === 'destroy') this.remove(model, options);
Run Code Online (Sandbox Code Playgroud)
所以,是的,我认为你不需要明确地从你的集合中删除模型.
但不要相信我,为自己测试:)
deleteTask: function() {
that = this;
this.model.destroy({
success: function() {
console.log(that.parent.collection);
}
});
}
Run Code Online (Sandbox Code Playgroud)
检查控制台,看看模型是否已从集合中删除.