BackboneJS - Events


Advertisements

Events are capable of binding objects and trigger custom events i.e. you can bind the custom events by using the desired name of our choice.

The following table lists down all the methods which you can use to manipulate the BackboneJS-Events −

S.No. Methods & Description
1 on

It binds an event to an object and executes the callback whenever an event is fired.

2 off

It removes callback functions or all events from an object.

3 trigger

It invokes the callback functions for the given events.

4 once

It extends the backbone.Model class while creating your own backbone Model.

5 listenTo

It informs one object to listen to an event on another object.

6 stopListening

It can be used to stop listening to events on the other objects.

7 listenToOnce

It causes the listenTo occur only once before the callback function is being removed.

Catalog of Built-in Events

BackboneJS allows the use of global events wherever necessary in your application. It contains some of the built-in events with arguments as shown in the following table −

S.No. Events & Description
1

"add"(model, collection, options)

It used when a model is added to the collection.

2

"remove"(model, collection, options)

It removes a model from the collection.

3

"reset"(collection, options)

It is used to reset the collection content.

4

"sort"(collection, options)

It is used when a collection needs to resorted.

5

"change"(model, options)

It is used when changes are to be made to a model's attributes.

6

"change:[attribute]"(model, value, options)

It is used when there is an update in an attribute.

7

"destroy"(model, collection, options)

It fires when the model is destroyed.

8

"request"(model_or_collection, xhr, options)

It is used when a model or a collection starts requesting to the server.

9

"sync"(model_or_collection, resp, options)

It is used when a model or a collection is synced successfully with the server.

10

"error"(model_or_collection, resp, options)

It activates when there is an error in requesting to the server.

11

"invalid"(model, error, options)

When there is a fail in model validation, it returns invalid.

12

"route:[name]"(params)

When there is a specific route match, this event can be used.

13

"route"(route,params)

It is used when there is a match with any route.

14

"route"(router, route, params)

It is used by history when there is a match with any route.

15

"all"

It fires for all the triggered events by the passing event name as the first argument.

Advertisements