我是否可以强制对模型属性进行更新,以将其注册为更改(即使不是更改)? [英] Can I force an update to a model's attribute to register as a change even if it isn't?
问题描述
我知道我可以设置Backbone模型的属性值,以使其不会使用 {silent:true} 触发 change 事件.我也知道,如果将模型的属性设置为已经具有的值,则不会触发更改事件,这几乎总是一件好事.但是,是否有一种方法可以强制对模型进行更新以触发更改事件,即使将其设置为相同的值也是如此?
I know that I can set the value of a Backbone model's attribute so that it doesn't trigger a change event using {silent:true}. I also know that if I set a model's attribute to a value it already has, it won't trigger a change event, which is almost always a good thing. However, is there a way to force the update to the model to trigger a change event even if it's set to the same value?
因此,如果在我的模型中,我已设置:
So if within my model I have set:
defaults:{
attributeToChange: "myValue"
}
然后在使用此模型的视图中调用:
And then in the a view using this model I call:
this.model.set('attributeToChange', 'myNewValue', {silent:true});
该值将更改,但不会触发任何更改事件.但是,如果我希望在设置属性值时无论其设置为什么,都将引发更改事件该怎么办?所以如果我只是做
the value will change but no change event will be fired. But what if I wanted to have a change event fired whenever the attribute's value was set, regardless of what it was set to? So if instead I just did
this.model.set('attributeToChange','myValue');
是否可以添加一个参数来强制将其视为更改?我相信我可以使用
is there a parameter I can add that will force this to be seen as a change? I believe that I can use
this.model.trigger('change:attributeToChange');
但我想知道是否有一种方法可以在没有此情况的情况下触发更改事件.
but I'd like to know if there's a way to force the change event to fire without this.
奖励问题:如果我将模型的属性设置为已经具有的值,这是否会使属性的 previous 值成为发起更改之前的值?
Bonus question: If I set a model's attribute to a value it already has, does this make the attribute's previous value the value it had before I initiated the change?
this.model.set('attributeToChange','someValue');
this.model.set('attributeToChange','anotherValue');
this.model.set('attributeToChange','anotherValue');
console.log(this.model.previous('attributeToChange')); //someValue or anotherValue?
推荐答案
在Backbone中为模型设置值时,Backbone会将新值与当前值进行比较,并将更改后的值添加到数组中(请参见 https://github.com/documentcloud/backbone/blob/master/backbone. js#L342 ).
When you set a value to a model in Backbone, Backbone will compare the new value to the current ones, and add the changed values to an array (see https://github.com/documentcloud/backbone/blob/master/backbone.js#L342).
然后,如果changes
数组包含元素,并且如果未将silent设置为true,则Backbone将为每个更改的项目触发"change:{key}"事件. (请参见 https://github.com/documentcloud/backbone/blob/master/backbone.js#L357 ).
Then, if the changes
array contains element, Backbone will triggers the "change:{key}" event for each changed item if silent is not set to true. (see https://github.com/documentcloud/backbone/blob/master/backbone.js#L357).
最后,它还将触发全局更改"事件(同样,如果silent没有设置为true).
Finally, it will also trigger the global "change" event (again, if silent is not set to true).
因此,如果要在未更改的属性上强制触发,则可以像Backbone一样手动触发事件,也可以像@Loamhoof建议的那样覆盖set方法.
So if you want to force the trigger on non-changed attributes, you can manually trigger the event, like Backbone does, or override the set method like @Loamhoof suggested.
这篇关于我是否可以强制对模型属性进行更新,以将其注册为更改(即使不是更改)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!