Backbone.js Events

 

The view displays the data of the model when an event occurs, and thus event is a vital part of an application. The attribute of the events in Backbone.JS to get mixed to any object is an advantage. The binding of the custom events can be done by using the name of a choice. The Backbone.JS Events’ methods are listed below:

 

METHOD USES
on To bind an event to an object and to execute the callback when an event is fired.
off To remove all the events or the callback functions from an object.
trigger To invoke the callback functions for the given events.
once To extend the Backbone.model class while a backbone model is being created.
listento To inform one object to listen to another object’s event.
stoplistening To stop listening to another object’s event.
listentoonce To cause the listento method to occur only once before the removal of the callback function.

 

Backbone.js Built-in Events:

Backbone.js facilitates some of the built-in events to allow the user to get access to global events when needed. Some of them are listed below:

 

EVENTS USES
“add”(model, collection, options) To add model in a collection.
“all” To fire for all triggered events by passing event name as first argument.
“change”(model, options) To make changes in a model.
“change:[attribute]”(model, value, options) To make updates in a model attribute.
“destroy”(model, collection, options) To be fired when model is destroyed.
“error”(model_or_collection, resp, options) To be activated when there is an error in server request.
“invalid”(model, error, options) To return invalid when a failure occurs in model validation.
“remove”(model, collection, options) To remove a model from a collection.
“reset”(collection, options) To reset the collection contents.
“request”(model_or_collection, xhr, options) To be fired when model or collection starts requesting to the server.
“route:[name]”(params) To be fired when there is a specific route match.
“route”(route,params) To be fired when there is a match with any route.
“route”(router, route, params) To be utilised by history when there is a match with any route.
“sort”(collection, options) To be fired when there is a need for collection to resort.
“sync”(model_or_collection, resp, options) To be fired when model or collection successfully sync with the server.