“改变”的绑定在骨干模型中不起作用
Here's the Example
I was following this excellent tutorial by Thomas Davis : What is a model?
Somehow the 'change' binding is not firing. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Backbone 正在检查设置的值是否与之前的值相同(查看 https://github .com/documentcloud/backbone/blob/master/backbone.js#L210 等)。
在您的示例中,数组仍然相同,但内部的值发生了变化。这个问题很难解决。创建数组的新副本似乎是开销。我建议直接在您的采用函数中调用更改事件作为解决方案:
我建议在主干 github 存储库上创建一个问题,也许添加一个“强制”选项来强制更新(从而触发事件)属性模型。
Backbone is checking if the set value is the same as the previous value (look at https://github.com/documentcloud/backbone/blob/master/backbone.js#L210 and on).
In your example, the array is still the same but the value inside changed. This is tricky to solve. Creating a new copy of the array seems to be overhead. I would suggest to call the change event directly in your adopt function as a solution:
I would suggest to create an issue on backbone github repository to maybe add a "force" option to force the update (thus triggering the event) of attributes on a model.
这是一个有点尴尬的解决方案:
Here is a bit awkward solution:
我们可以将其用作集合并侦听集合的添加、删除事件,而不是将孩子用作普通数组。
Instead of using children as an plain array we can use it as an collection and listen to the add,remove events of the collection.