gpt4 book ai didi

backbone.js - 在 Backbone.js 中,为什么静默更改最终会触发更改事件?

转载 作者:行者123 更新时间:2023-12-03 10:26:47 25 4
gpt4 key购买 nike

当我通过{"silent":true}在 Backbone 模型中设置属性时,为什么不只是抑制 change:attribute事件?下次更改属性时触发该事件有什么好处?

更新

Backbone 0.9.10 改变了传递行为 { "silent": true } .从变更日志:

Passing {silent:true} on change will no longer delay individual "change:attr" events, instead they are silenced entirely.



浏览变更日志 here

最佳答案

这也让我困惑了一段时间。

原因是 {silent:true} 确实 不是 意思是“一切正常,但不要触发事件”。

从@jashkenas 的各种评论和答案来看,它的意思似乎是“只需更改属性值(并将其添加到 'changedAttributes' 哈希中),但将所有其他“与更改相关”的事件推迟到以后”。

'silent' 不会阻止 change对于那个/那些属性的事件,它只是将“公告”排队,直到下一个 change事件被触发。

所以,它可能更好地命名为 defer .

相关信息:

https://github.com/documentcloud/backbone/pull/850

the point of a "silent" change is that it isn't considered a change from the models point of view. Later, when the change actually occurs, you get the full difference all at once.



https://github.com/documentcloud/backbone/issues/870

Because the point of silent changes is that you're allowed to twiddle with the internal state of your model, temporarily, without actually making a change. Later, when the attribute actually changes, the validation runs and events are emitted. It wouldn't make sense to emit an error event when making a silent change.



2013 年 4 月 7 日更新

注意:我没有对此进行测试以确认行为,这只是基于我对发行说明的阅读......

从 Backbone 0.9.10 开始,上述行为发生了变化。在那个版本(和更新版本)中, silent:true抑制 change:attr事件完全 - 不仅仅是延迟它们。

http://backbonejs.org/#changelog

关于backbone.js - 在 Backbone.js 中,为什么静默更改最终会触发更改事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10030139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com