[英]Why does my Backbone model hasChanged() always return false?
Given the following snippet: 给出以下代码段:
var m = new Backbone.Model({
name: 'joshua'
});
m.set('name', 'something else');
If I now call m.hasChanged()
or m.hasChanged('name')
I get false
. 如果我现在调用
m.hasChanged()
或m.hasChanged('name')
我就会变false
。 Why? 为什么? My understanding is that both of these should return
true
. 我的理解是这两者都应该归结为
true
。
m.changedAttributes()
also returns false. m.changedAttributes()
也返回false。
Here is a fiddle that illustrates what I'm doing, and expecting: http://jsfiddle.net/9cvVv/88/ 这是一个小提琴,说明我正在做什么,并期待: http : //jsfiddle.net/9cvVv/88/
EDIT: It seems that unless you pass { silent: true; }
编辑:似乎除非你通过
{ silent: true; }
{ silent: true; }
to the set()
method then it will fire the change
event on your model which clears out the changedAttributes()
, etc. Essentially these properties only track changes since the last time the change
event was triggered. { silent: true; }
的set()
方法,那么将触发change
你的模型事件,送走changedAttributes()
等。从本质上讲这些属性只跟踪自从上次的变化change
事件被触发。
I read that in the documentation but didn't really understand it at first. 我在文档中读到了它,但起初并没有真正理解它。
This doesn't seem very useful to me. 这对我来说似乎没什么用。 I would appreciate any explanation of why this works the way it does and the best way to achieve the result I want.
我将不胜感激任何解释为什么它的工作方式和实现我想要的结果的最佳方式。 (Passing around
{silent: true; }
and giving up usage of the change
event seems like a waste.) (绕过
{silent: true; }
并放弃使用change
事件似乎是浪费。)
Unless you pass { silent: true; }
除非你通过
{ silent: true; }
{ silent: true; }
to the set()
method then it will fire the change
event on your model which clears out the changedAttributes()
, etc. Essentially these properties only track changes since the last time the change
event was triggered. { silent: true; }
的set()
方法,那么将触发change
你的模型事件,送走changedAttributes()
等。从本质上讲这些属性只跟踪自从上次的变化change
事件被触发。
So the answer is to call this instead: 所以答案是改为:
m.set('name', 'something else', {silent: true})
This post is premised on the previous behavior of older versions of Backbone. 这篇文章的前提是旧版Backbone的旧行为。
hasChanged
does now ( as of 0.9.10 ) always returns true
after set(..)
is called. hasChanged
现在这样 ( 为0.9.10 )总是返回true
后set(..)
被调用。 The silent
flag no longer has any effect. silent
旗帜不再有任何影响。
This is confusingly masked in the jsfiddle linked in the question which uses a CDN-hosted copy of backbone.js which always uses the latest version. 这在使用CDN托管的backbone.js副本的问题链接的jsfiddle中被混淆地掩盖了,该副本总是使用最新版本。 Here's some updated jsfiddles showing the change in behavior:
这是一些更新的jsfiddles,显示行为的变化:
Model.set()
takes an attributes hash as the first argument. Model.set()
将属性hash作为第一个参数。 Try m.set({'name': 'something else'});
尝试
m.set({'name': 'something else'});
. 。 Doing
m.set('name', 'something')
doesn't set 'name', so it never triggers the change
event and your call to hasChanged()
returns false
. 执行
m.set('name', 'something')
不设置'name',因此它永远不会触发change
事件,并且你对hasChanged()
调用返回false
。 You can always inspect the current value of the attributes hash by logging out m.attributes
- though accessing and manipulating it directly isn't recommended as none of the change events will fire that way. 您始终可以通过注销
m.attributes
来检查属性哈希的当前值 - 虽然不建议直接访问和操作它,因为任何更改事件都不会以此方式触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.