Highway. Is it possible to associate a change event with a model, with the exception of one property?

I want the change event to fire at any time when I change any property of the model except one. Is it possible? Besides:

model.bind('change:prop1', func); model.bind('change:prop2', func); model.bind('change:prop3', func); etc.... 

Or, alternatively, is there a way to find out which property caused the change from the event handler?

thanks

+7
source share
3 answers

You can use model.bind('change',function() {/*...*/}) , and in the function use hasChanged to check the attributes: if(model.hasChanged('propIWantToExclude')) return;

+11
source

Justin's answer above will return when "propIWantToExclude" and some other attributes are changed together. You probably don't want to do this, so you should also look at the size of model.changedAttributes :

 if(model.changedAttributes.length == 1 && model.hasChanged('attrIWantToExclude')) { return; } 
+3
source

Responding to David Tuit’s request to answer the first part of the question, you can configure the function to respond to a β€œmodified” event, and then trigger a custom event if the property that you want to ignore has not been changed.

This logic will trigger a custom event: 'somePropertyOtherThanThePropIWantToExcludeChanged' if the property has not been changed. If several properties have been changed, including the one you want to ignore, then the custom event will NOT fire either:

 model.bind('change', function(){ if( !model.hasChanged('propIWantToExclude') ){ model.trigger('somePropertyOtherThanThePropIWantToExcludeChanged'); } }); 
0
source

All Articles