Open In App

Backbone.js off Event

Last Updated : 13 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Backbone.js off Event is used to remove the previously bound callback function to an object. It removes all the callbacks if no context is specified. If no callback is specified all the event’s callbacks are removed. If no event is specified all the callbacks for all the events are removed. 

Syntax: 

Object.off ( event, callback, context ) ;

Parameter Values: It accepts the following parameters:

  • event: It is used to bind an object with an event.
  • callback: It is executed when an event is invoked.
  • context: It is an object which is passed to a callback function.

Example 1: In this example, we will describe the Backbone.js off Event. Here we will see how we remove all the callbacks for any particular event.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>BackboneJS Model off</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;">
        GeeksforGeeks
    </h1>
  
    <h3>BackboneJS Model off</h3>
  
    <script type="text/javascript">
  
        var myVal = Backbone.Model.extend(
              { name: 'Hello Guys' }, Backbone.Events);
        var myFunc = function () {
            document.write('This is by first callback <br>');
        };
  
        var myFunc1 = function () {
            document.write('This is by second callback <br>');
        };
  
        myVal.on('log', myFunc);
        myVal.on('log', myFunc1);
        document.write("Before Removing all "
            + "callbacks for log events. <br>");
        myVal.trigger('log');
  
        myVal.off('log');
  
        document.write("<br>After Removing all callback"
            + " for log events nothing will print <br>");
        myVal.trigger('log');  
    </script>
</body>
  
</html>


Output:

Backbone.js off Event

Example 2: In this example we will remove specific callback for all the events and Remove all the callback of model with all the events.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>BackboneJS Model off</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;">
        GeeksforGeeks
    </h1>
  
    <h3>BackboneJS Model off</h3>
  
    <script type="text/javascript">
  
        var myVal = Backbone.Model.extend(
              { name: 'Hello Guys' }, Backbone.Events);
        var myFunc = function () {
            document.write('This is by first callback <br>');
        };
  
        var myFunc1 = function () {
            document.write('This is by second callback <br>');
        };
  
        var myFunc2 = function () {
            document.write('This is by third callback <br>');
        };
  
  
        myVal.on('log', myFunc);
        myVal.on('log', myFunc1);
        myVal.on('log', myFunc2);
        document.write("Before Removing myFunc callbacks"
            + " for all events. <br>")
        myVal.trigger('log');
  
        myVal.off(null, myFunc);
        document.write("<br>After Removing all callback "
            + "for log events nothing will print <br>")
        myVal.trigger('log');
  
        myVal.off();
        document.write("<br> Remove all the "
            + "callbacks of myval model ");
        myVal.trigger('log');
    </script>
</body>
  
</html>


Output:

Backbone.js off Event

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads