Wednesday, July 3, 2024
HomeLanguagesJavascriptWhat are Backbone.js Events ?

What are Backbone.js Events ?

Events in backbone.js play a very important role, technically it is a module that can be integrated into an object which gives object ability to bind and trigger accordingly to custom events. Generally, events are not declared before they are even bound they are simply passed arguments.

In simple words, backbone.js events are a special type of listeners that respond to any changes made.

Syntax (backbone events implementation by extending the class):

var object = {};
_.extend(object, Backbone.Events);
// Write your events here

Parameter description:

  • Object: It is an object created for class events we can give whatever object name we want to use.
  • Backbone.Events: It is a class in the backbone called events.

There are various methods associated with Backbone Events they are as follows:

  • on: This method is used to bind an event to an object, it executes the callback whenever an event is fired. 
  • off: This method is used to remove all the events and callback functions from an object.
  • trigger: This method is used to invoke the callback functions for provided set of events.
  • once: This method is used to extend the Backbone.js model class while creating a backbone model.
  • listenTo: This method is used to make an object listen to another object’s event.
  • stopListening: This method is used to stop listening to the event of another object.
  • listenToOnce: This method is used to cause the listento method to occur only once before the callback function is removed.

Backbone.js in-built events:

  • “add” (model, collection, options): This event is used to add a model to the collection.
  • “remove” (model, collection, options): This event is used for the removal of a particular model from the collection.
  • “update” (collection, options): This event is used to the single event triggering when a particular number of models are removed/added/changed from a collection.
  • “reset” (collection, options): This event is used to reset the total contents of a collection.
  • “sort” (collection, options): This event is used to re-sort the collection.
  • “change” (model, options): when we want to change the attributes of a model this event is used.
  • “changeId” (model, previousId, options): when we want to update the id of a particular model, we use this method.
  • “change:[attribute]” (model, value, options): when there is a change detected in the attribute this event is automatically triggered.
  • “destroy” (model, collection, options): when we want to destroy a model from a collection this method is used.
  • “request” (model_or_collection, xhr, options): For processing the request of the server from the collection, model this event is used.
  • “sync” (model_or_collection, response, options): For syncing the model and collection with the server this event is used.
  • “error” (model_or_collection, xhr, options): This event is invoked when there is a failure to server request created by the model or collection.
  • “invalid” (model, error, options): Whenever the validation fails for the model on the client side this event is invoked.
  • “route:[name]” (params): When a specific route is found by the router this event is fired.  
  • “route” (route, params): When a route is matched by a router this event is fired.
  • “route” (router, route, params): When a route is matched this event is automatically triggered or fired by history.
  • “all”: This method fires for every event that is triggered by passing argument as the event name.

Example 1: In this example, we are using on event.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Backbone.js event example</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
    <h3>Backbone.js On event</h3>
    <script type="text/javascript">
        var value = _.extend({
            name: 'neveropen'
        }, Backbone.Events);
        value.on('first', function () {
            document.write(
                "Trigger-1) The triggered value is: ");
            document.write(this.name);
        });
        value.on('second', function () {
            document.write("<br>");
            document.write(
                "Trigger-2) The triggered value is: ");
            document.write(this.name);
        });
        value.trigger('first');
        value.trigger('second');
    </script>
</body>
  
</html>


Output:

 

Example 2: In this example, we are using once event.

HTML




<!DOCTYPE html>
<html>
    
<head>
    <title>Backbone.js event example</title>
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <h3>Backbone.js example using Once event</h3>
      
    <script type="text/javascript">
        var obj = {};
        _.extend(obj, Backbone.Events);
        obj.once('trigger', function () {
            document.write(
                `<h2 style="color:blue;">
                    OUTPUT AFTER TRIGGERING ONCE EVENT:
                </h2>`);
            alert("triggered");
        });
        obj.trigger('trigger');
        obj.trigger('trigger');
    </script>
</body>
  
</html>


Output:

 

Reference: https://backbonejs.org/#Events

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments