Explain Built-in Events in Backbone.js


There are various events that are present in Backbone.js and these events are called built-in Events. As the name suggests programmers and users already define built-in events and can directly use them. Also as per the requirements, a user can create his/her own event which doesn’t comes under the built-in Events.

A remarkable quality that an event provides a user is that it adds the event listeners to the related selector, custom selectors, el. Also if none of the selection is provided then both the related selector and el event listeners are attached.

Backbone.js Introduction

Backbone.js is a toolkit or framework for the Javascript programming language that allows for lightweight single-page apps to be created. Additionally, BackboneJS is a small JavaScript framework that enables the development and administration of client-side web applications. It offers an MVC framework that abstracts DOM into views and data into models before using events to link the two.

The work of Backbone.js is by providing models with key-value binding and custom events, collections with a comprehensive API of enumerable methods, views with declarative event handling, and connecting all of these components to your current API via a RESTful JSON interface, Backbone.js brings structure to web applications.

Events in Backbone.js

Backbone.js Events are the modules that can be added to any object. By using our suggested names, it makes it simpler for objects to bind to and initiate custom events. Furthermore, when an event occurs, it connects to the object and initiates the callback. It removes each and every event and callback from an object. It turns on the callback functions for the specified events. It makes the backbone longer.

The following is a list of techniques for modifying the Backbone.js Events −

  • off − It eliminates all events or callbacks from an object.

  • trigger − It activates the callback processes for the events specified.

  • on − When an event is fired, it ties the event to the object and calls the callback.

  • stopListening − This function allows you to halt listening to events occurring on other objects.

  • listenTo − It alerts one object to communicate with another object.

  • listentoonce − It limits the number of times the listento occurs before the callback function is terminated.

  • once − When developing your own backbone model, it extends the backbone.model class.

Syntax of the events in the backbone.js

Events in the backbone.js are defined in the key pairs with the help of the events keyword. Let’s see the syntax first for more understanding −

events: {
   'first_event_name': 'function_first',
   'second_event_name': 'function_second',
}

In the above syntax, we will define an event using the ‘events’ keyword and in that, we will define a particular event in the key-pair form with the name of the event followed by the function to call after the event is called.

Built-in Events in Backbone.js

We have seen the basics of the events in the backbone.js like some are built-in while some are not. Also after moving further we have seen some of the syntaxes of the events that the user can create by himself. Now let’s move to the basics of the built-in events of the backbone.js −

On Event

Syntax

object.on(event, callback_function, [context])

This event binds a callback_function to the given object which has called it and whenever the event is fired the callback function will be fired. Also, context is not a compulsory parameter but the other two parameters are compulsory to pass.

For Example

student.on("change: name, change:roll_number", fun_change);

In the above example, we have an object that is a student and for that student, we call the function ‘fun_change’ when the event is to change the name and roll number.

Off Event

Syntax

object.off([event], [callback_function], [context])

In the previous event, we added a particular callback function with the object, to remove that callback function from the object we can use this event. Here all the parameters are optional to pass but if the user wants to pass the context then the callback function and event must be present there, similarly, if the user doesn’t want to pass the context but want to go with the callback function then the event must be present there.

If the context is not provided then all the objects with the same callback function the event will be removed, similarly, if the event is not specified then the callback function for all the events will be removed.

Trigger Event

Syntax

object.trigger(event, [*args])

In the above syntax, an event parameter is compulsory to pass, and more than one event can be passed which will be divided using the comma. Other specific parameters can be provided if needed but they are not compulsory. Trigger just triggers the callbacks of the given event or the set of events passed as the argument to this method.

Once Event

Syntax

object.listenTo(other,event, callback)

Here all the parameters are compulsory to pass and this event tells the object which calls it to listen to the other event and this helps to keep track of all the events which are added to the object.

StopListening Event

Syntax

object.stopListening([other],[event], [callback])

Here all the parameters are not compulsory and all are optional. If removing all the events from the object to which it was previously listening according to the parameters provided.

Conclusion

In this article, we have learned that there are various events that are present in Backbone.js and these events are called built-in Events. Backbone.js is a toolkit or framework for the Javascript programming language that allows for lightweight single-page apps to be created. when an event occurs, it connects to the object and initiates the callback. It removes each and every event and callback from an object. Various events are: on, off, trigger, once, listenTo,

Updated on: 17-Mar-2023

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements