Correct way of binding multiple attribute changes to a Backbone.js model Correct way of binding multiple attribute changes to a Backbone.js model javascript javascript

Correct way of binding multiple attribute changes to a Backbone.js model


As of Backbone.js 0.9.0, the bind() function (which has been renamed to on()) supports a space-delimited list of events:

model.on("change:title change:author", ...)// equivalent tomodel.bind("change:title change:author", ...)


I don't know if such a "bulk-bind" function exists (you could open a feature request for it, it seems useful).

You can bind them separately:

var Mine = Backbone.Model.extend({  initialize: function() {    var listener = function() { console.log('changed'); };    this.bind("change:attribute_1", listener);    this.bind("change:attribute_2", listener);  }});

Or you can listen to all changes (and then filter in the listener):

var Mine = Backbone.Model.extend({  initialize: function() {    var listener = function() { console.log('changed'); };    this.bind("change", listener);  }});