9951 explained code solutions for 126 technologies


backbone.jsHow do I use a trigger in Backbone.js?


A trigger in Backbone.js is a way to bind a callback to a specific event. It is used to respond to changes in the models.

For example:

var model = new Backbone.Model();

model.on("change", function() {
  console.log("Model has changed!");
});

model.set("name", "John");

// Output: Model has changed!

In the example code above, a trigger is set on the model using model.on("change", function() {...}). Whenever the model's name attribute is changed, the callback function will be executed.

The parts of the code are:

  • var model = new Backbone.Model();: This creates a new instance of the Backbone model.
  • model.on("change", function() {...}): This sets the trigger on the model.
  • model.set("name", "John"): This changes the model's name attribute and triggers the callback.

For more information about triggers in Backbone.js, see the Backbone.js documentation.

Edit this code on GitHub