Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone collection change event on model

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'

like image 452
praks5432 Avatar asked Dec 10 '25 10:12

praks5432


1 Answers

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();
    }

}
like image 97
namero999 Avatar answered Dec 12 '25 00:12

namero999



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!