防止单个属性触发 this.bind(“change”)
在 Backbone.js 中,我有一个将更改事件绑定到的模型,但我想防止在特定属性更改时发生这种情况。例如,我希望它在每次调用 model.set()
时触发,除了调用 model.set({atoryName: value}) 时
。
这就是我所拥有的:
this.bind("change", function() {
this.update();
});
但我不知道如何确定正在设置的内容 - 有什么想法吗?
编辑
看起来我可以调用
model.set({arbitraryName: value}, {silent: true})
以防止触发更改事件(这符合我的需要),但是如果我有类似的绑定怎么办:
this.bind("change:arbitraryName", functionName)
In Backbone.js, I have a model I am binding a change event to, but I want to prevent this from happening on specific attribute changes. For example, I want it to fire for every single time model.set()
is called, except when calling model.set({arbitraryName: value})
.
Here's what I have:
this.bind("change", function() {
this.update();
});
But I have no clue how to determine what is being set--any ideas?
EDIT
It looks like I can call
model.set({arbitraryName: value}, {silent: true})
to prevent the change event from firing (which works for what I need), but what if I have something bound like:
this.bind("change:arbitraryName", functionName)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以考虑在事件处理程序中使用
hasChanged
吗?我不确定我完全理解你的问题。请注意上面和下面的区别。
第一个将在
someAttribute
保持不变的任何更改上触发update
。第二个将在对someAttribute
进行任何更改时触发更新。希望这有帮助。
You can consider using
hasChanged
in the event handler?I'm not sure I understand your question completely. Please notice the difference of the above, and the below.
The first one will fire
update
on any change wheresomeAttribute
remains constant. The second one will fire update on any change tosomeAttribute
.Hope this helps.