9951 explained code solutions for 126 technologies


backbone.jsHow do I add a change event handler to a Backbone.js object?


Adding a change event handler to a Backbone.js object is simple. First, create a Backbone.js object and assign it to a variable:

var myObject = new Backbone.Model({
    name: 'John',
    age: 30
});

Then, use the on() method to bind a change event handler to the object:

myObject.on('change', function() {
    console.log('The object has changed!');
});

This will cause the event handler to be invoked whenever the object's data changes. To test this, we can change the object's data and see the output:

myObject.set('name', 'Bob');
// Output: The object has changed!

The on() method takes two parameters: the event name (in this case, change) and the event handler function. The event handler function will be invoked with the object as its first parameter.

Code explanation

  1. var myObject = new Backbone.Model({ name: 'John', age: 30 }); - Create a Backbone.js object and assign it to a variable.
  2. myObject.on('change', function() { console.log('The object has changed!'); }); - Bind a change event handler to the object.
  3. myObject.set('name', 'Bob'); - Change the object's data.

Helpful links

Edit this code on GitHub