这是Example
我遵循了Thomas Davis的这篇优秀的教程:What is a model?不知何故'change‘绑定不能触发。我在这里做错了什么?
发布于 2011-04-26 02:55:59
Backbone正在检查设置值是否与之前的值相同(查看https://github.com/documentcloud/backbone/blob/master/backbone.js#L210和on)。
在您的示例中,数组仍然是相同的,但其中的值发生了变化。这很难解决。创建数组的新副本似乎是一种开销。我建议在采用函数中直接调用change事件作为解决方案:
adopt: function(newChildsName){
var children_array = this.get('children');
children_array.push(newChildsName);
this.set({children:children_array});
this.trigger("change:children");
}我建议在backbone github存储库上创建一个问题,可能会添加一个" force“选项来强制更新(从而触发事件)模型上的属性。
发布于 2011-04-28 22:04:09
这是一个有点尴尬的解决方案:
adopt: function(newChildsName){
var children_array = this.get('children').splice(0);
children_array.push(newChildsName);
this.set({children:children_array});
}发布于 2011-06-27 18:05:09
我们可以将它用作集合,并监听集合的添加、删除事件,而不是将子数组用作普通数组。
https://stackoverflow.com/questions/5777232
复制相似问题