I would like a change event to fire anytime I change any property of the model, except for one. Is this possible? Besides doing:
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 triggered the change from within the event handler?
You could use model.bind('change',function() {/*...*/})
and in the function use hasChanged
to check the attributes: if(model.hasChanged('propIWantToExclude')) return;
Justin's answer above will return when 'propIWantToExclude' and some other attributes are changed together. You probably don't want to do that, so you should also look at the size of model.changedAttributes
:
if(model.changedAttributes.length == 1 && model.hasChanged('attrIWantToExclude')) {
return;
}
Responding to David Tuite's request to answer the first part of the question, you could set up a function to respond to the "changed" event and then trigger a custom event if the property that you want to ignore was not changed.
This logic would trigger the custom event: 'somePropertyOtherThanThePropIWantToExcludeChanged'
if the property was not changed. If multiple properties were changed, including the one you want to ignore, then the custom event would also NOT fire:
model.bind('change', function(){
if( !model.hasChanged('propIWantToExclude') ){
model.trigger('somePropertyOtherThanThePropIWantToExcludeChanged');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With