How to add parameters or variables to a Backbone view

It’s very easy to add parameters or variables to a Backbone view but being aware of what BackboneJS version are you using because it changed. Let’s see:

Add parameters or variables to a Backbone view

Before 1.1.0 version

Any parameter in the constructors went to the options variable of the view::

Controller

var userView = new UserView({ mode: "add" })

View

render: function() { 
 // Use example in any function 
 var mode = this.options.mode; 
 // mode="add" 
}

Now (version >= 1.1.0)

Just like above but adding this to the Controller:

initialize : function (options) { 
  this.options = options || {}; 
}

You can check the BackboneJS changelog

David Burgos

Read more posts by this author.