Binding DOM Event With View Method in Backbone.JS


In this article, I will show you how to bind DOM events with the View method in backbone.js. At the time of writing, to use backbone.js we need to add three dependencies, the first is the "jQuery" library, the second is the "Underscore.js" library and the last is the Backbone.js library. This associates the click event to the button on the DOM.
In this article, you can see that when we click on the button then one item is added to the list. It will be done an infinite number of times. Here DOM events are bound to the View method. In the backbone, there is no separate controller to handle this type of binding, here every event happens in the View.
Now see the example:
  1. First, create an application for using this binding.
  • Start Visual Studio 2013.
  • From the Start window select "New Project".
  • Select "Installed" -> "Template" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and select "ASP.NET Empty Web Application". 
Create Web Application
  • Click on the "OK" button.
  1. Now Add an HTML page in the project:
  • In the Solution Explorer.
  • Right-click on the project and select "Add" -> "HTML Page".
Select HTML page
  • Change the name of the page.
Change the name
  • Click on the "Add" button.
Write this code
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head> <meta charset="utf-8">  
  4. <title>Add List Item</title>  
  5. </head>  
  6. <body>  
  7. <script src=""></script>  
  8. <script src=""></script>  
  9. <script src=""></script>  
  10. <script src=""></script>  
  11. <script src="AddList.js" type="text/javascript"></script>  
  12. </body>  
  13. </html> 
  1. Now add the scripting file to the project:
  • In the Solution Explorer.
  • Right-click on the project then select "Add" -> "New Item" -> "JavaScript".
Add javaScript page
  • Click on the "Add" button.
Add the following code in the scripting file:
  1. (function($){  
  2. var ListView = Backbone.View.extend({  
  3. el: $('body'),  
  4. events: {  
  5. 'click button#add''addItem'  
  6. },  
  7. initialize: function(){  
  8. _.bindAll(this'render''addItem');  
  9. this.counter = 0;  
  10. this.render();  
  11. },  
  12. render: function(){  
  13. $(this.el).append("<button id='add'>Click To Add List</button>");  
  14. $(this.el).append("<ul></ul>");  
  15. },  
  16. addItem: function(){  
  17. this.counter++;  
  18. $('ul'this.el).append("<li>Welcome"+this.counter+"</li>");  
  19. }  
  20. });  
  21. var listView = new ListView();  
  22. })(jQuery); 
In the code above are the functions render() and addItem().
The render() function specifies to the button to add the new item in the list.
The addItem() method is called when you click on the button.
  1. Now set the HTML page as Execute, first by setting the property using "Set as start project".
  • Right-click on the "AddListItem.html".
  • Then select "Set as start page".
Set html as start page
  1. Execute  the application, this is displayed as a button:

Display a button
When the button is clicked the new item will be added to the list.
Add list Item