Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force an update to a model's attribute to register as a change even if it isn't?

Tags:

backbone.js

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.

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?
like image 670
flyingace Avatar asked Apr 10 '13 16:04

flyingace


People also ask

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What is OnModelCreating in entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.

What is HTML ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


1 Answers

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).

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).

Finally, it will also trigger the global "change" event (again, if silent is not set to true).

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.

like image 185
Cyril N. Avatar answered Oct 31 '22 22:10

Cyril N.