Add new comment

Events

Submitted by Erik Wegner on Fri, 02/21/2014 - 17:41

Backbone helps your application to react on data changes. There are ways to listen to attribute changes (e. g. a task gets completed) or object creation and destruction.

The base for all event handling is within the class Backbone.Events. Any derived class can trigger events. Other objects can register (by using the functions listenTo,on or once) to be notified if any event occurs.

There is a good example in the backbone documentation, which I repeat here:

var object = {};

_.extend(object, Backbone.Events);

object.on("alert", function(msg) {
  alert("Triggered " + msg);
});

object.trigger("alert", "an event");

The classes View, Collection and Model do have all the Backbone.Event functionality.