Backbone collection change event on model
12,142
Solution 1
There's not a "shortcut" way of doing so. You have to listen to the normal change
event, and in your listener, see if the value has changed to something interesting for you. Then, propagate the event, fire a new one, or do stuff.
Backbone.Collection.extend({
initialize: function() {
this.on('change:property', this.onChange);
},
onChange: function(e) {
// sorry for pseudo-code, can't remember syntax by heart, will edit
if (e.newValue == true)
myLogic();
}
}
Solution 2
You cannot listen for an explicit value since that wouldn't work well in the general case, but you can easily bind to the general handler and run your code based on that.
var MyCollection = Backbone.Collection.extend({
initialize: function(models, options){
this.on('change:myProperty', this.changeMyProperty_, this);
},
changeMyProperty_: function(model, value){
if (value) this.myPropertyTrue_(model);
},
myPropertyTrue_: function(model){
// Do your logic
}
});
Author by
praks5432
Updated on June 04, 2022Comments
-
praks5432 almost 2 years
Is it possible to listen for a change in a model in a collection if a specific field is changed to a specific value?
I know that something like 'change:fieldName' exists, I'm looking for something like 'changeTo: fieldName = true'
-
fbynite over 10 yearsWhat's wrong with checking the value in your callback function?
-