“改变”的绑定在骨干模型中不起作用

发布于 2024-11-02 22:29:35 字数 194 浏览 0 评论 0原文

这是示例

我正在遵循 Thomas Davis 的这篇优秀教程:什么是模型? 不知何故,“更改”绑定没有触发。我在这里做错了什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-11-09 22:29:35

Backbone 正在检查设置的值是否与之前的值相同(查看 https://github .com/documentcloud/backbone/blob/master/backbone.js#L210 等)。

在您的示例中,数组仍然相同,但内部的值发生了变化。这个问题很难解决。创建数组的新副本似乎是开销。我建议直接在您的采用函数中调用更改事件作​​为解决方案:

adopt: function(newChildsName){
  var children_array = this.get('children');
  children_array.push(newChildsName);
  this.set({children:children_array});
  this.trigger("change:children");
}

我建议在主干 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:

adopt: function(newChildsName){
  var children_array = this.get('children');
  children_array.push(newChildsName);
  this.set({children:children_array});
  this.trigger("change:children");
}

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.

妄司 2024-11-09 22:29:35

这是一个有点尴尬的解决方案:

adopt: function(newChildsName){
  var children_array = this.get('children').splice(0);
  children_array.push(newChildsName);
  this.set({children:children_array});
}

Here is a bit awkward solution:

adopt: function(newChildsName){
  var children_array = this.get('children').splice(0);
  children_array.push(newChildsName);
  this.set({children:children_array});
}
泪之魂 2024-11-09 22:29:35

我们可以将其用作集合并侦听集合的添加、删除事件,而不是将孩子用作普通数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文