9951 explained code solutions for 126 technologies


backbone.jsHow do I initialize a view in Backbone.js?


To initialize a view in Backbone.js, you must first create a view object and then define its properties. The view object must extend the Backbone.View class, which provides the basic functionality for a view.

For example:

var MyView = Backbone.View.extend({

  // Properties
  el: "#view",
  template: _.template("<h1>My View</h1>"),

  // Initialize
  initialize: function() {
    this.render();
  },

  // Render
  render: function() {
    this.$el.html(this.template());
  }

});

// Create a new view
var view = new MyView();

The code above will create a new view object called MyView which is an extension of the Backbone.View class. It sets the el property to #view and the template property to a template string. It also defines an initialize function which calls render when the view is initialized. The render function then sets the HTML of the view's el to the template string. Finally, a new instance of the view is created and assigned to the view variable.

Parts of the code and explanation:

  • var MyView = Backbone.View.extend({ - This creates a new view object called MyView which is an extension of the Backbone.View class.
  • el: "#view" - This sets the el property to #view. This is the DOM element that the view will be attached to.
  • template: _.template("<h1>My View</h1>") - This sets the template property to a template string. This is the HTML that will be rendered by the view.
  • initialize: function() { - This defines an initialize function which is called when the view is initialized.
  • render: function() { - This defines a render function which sets the HTML of the view's el to the template string.
  • var view = new MyView(); - This creates a new instance of the view and assigns it to the view variable.

## Helpful links

Edit this code on GitHub