Categories
backbonejs javascript

A Simple Backbone.js View

This is our html which includes a template within script tags.

    
  
  
    
    A Simple Backbone.js View
  
  
    
    
    
         

      
 

This is our backbone code.

(function($){
    
    var MainView = Backbone.View.extend({
        el: $('#container'),
        
        initialize: function(){
         _.bindAll(this, 'render');                
            this.render();   
        },
        
        render:function(){
            // static object with our contact information. this will be used to populate our template.
            var contactDetailsObj = {
                name: "Aldo Lugo",
                phone: "123-456-7890",
                location: "Dallas, Texas",
                email: "john@email.com"               
            };
            var template = _.template( $("#contact-template").html(), contactDetailsObj );
            this.el.html(template);  
        }
        
    });
    
    var mainView = new MainView();
    
})(jQuery);

View this example on JSFilddle