Demonstrating Backbone.js: Listening to DOM events

Introduction

Welcome to the "Demonstrating Backbone.js" article series. This article demonstrates how to create and use collections in Backbone.js. This article starts with the concept of Backbone.js and various components of it. Previous articles have provided an introduction to views and the implementation of routers and collections. You can get them from the following:

In this article we will see listening to DOM events using views
 

 Backbone.js requires three dependencies , the Jquery Library , Underscore.js and Backbone.js libraries , this illustrates the simple task of associating a listener for the click event on the checkbox element on the DOM, Backbone.js provides all the necessary functionalities to make a fully fledge web applications , it is well known for the implementation of the MVc architecture .
 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.    
  6.     <script type="text/javascript" src="backbone/Jquery.js"></script>  
  7.     <script type="text/javascript" src="backbone/underscore-min.js"></script>  
  8.     <script type="text/javascript" src="backbone/backbone-min.js"></script>  
  9.       
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.         <script>  
  14.             $(document).ready(function () {  
  15.                 /*Enclose the script under Jquery Document.ready , this makes sure that the script is executed after the DOM   
  16.                 elements on the browser is loaded */  
  17.                 var View = Backbone.View.extend({  
  18.   
  19.                     el: '#listen_to_box',  
  20.                     /*associate  DOM element on which you want to work on ,   
  21.                     In this case #listen_to_box is the ID of the div element with the  checkbox */  
  22.   
  23.                     events: {  
  24.                         'click [type="checkbox"]': 'clicked',  
  25.                         /*associate an event listener 'clicked' for   
  26.                         the click event on the checkbox , this is done using the Views events property*/  
  27.                     },  
  28.                     clicked: function (event) { /*The Handler function for the click event on the checkbox*/  
  29.                         console.log("events handler for " + this.el.outerHTML);  
  30.                         /* el.outerHTML has the markup or the HTML code of the DOM element associated with el,   
  31.                             in this case the 'div' element with the id of #listen_to_box is assigned to el */  
  32.                     }  
  33.                 });  
  34.                 var view = new View();  
  35.   
  36.             });  
  37.   
  38.             </script>  
  39.        
  40.     <div id="listen_to_box">  
  41.         <input type="checkbox" />  
  42.     </div>  
  43.    
  44.           
  45.     </form>  
  46. </body>  
  47. </html>  
 
 
 
 
 Bind alert event to object 

 Here we will bind alert event to some object and then we will trigger the alert event.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.    
  6.     <script type="text/javascript" src="backbone/Jquery.js"></script>  
  7.     <script type="text/javascript" src="backbone/underscore-min.js"></script>  
  8.     <script type="text/javascript" src="backbone/backbone-min.js"></script>  
  9.       
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.         <script>  
  14.             var Product = {};  
  15.             _.extend(Product, Backbone.Events);  
  16.             Product.bind("alert", function (msg) {  
  17.                 alert("Triggered " + msg);  
  18.             });  
  19.             Product.trigger("alert", "Alert Event Trigger Fired");  
  20.   
  21.             </script>  
  22.   
  23.           
  24.     </form>  
  25. </body>  
  26. </html>  
 
 
 
 Summary

In this article, I explained how to use events in Backbone.js. In future articles we will understand more about Backbone.js with examples.
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all