Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js - passing arguments through constructors

Scenario:
I got an alert() saying undefined when I (try to) set the myVar variable through the Constructor. However if I uncomment the myVar that's living inside the myView, the alert then instead says "Hello from inside", just as one would expect.

Question:
Does this mean that I cannot set any params in the constructor of the view except backbones own params, such as model, collection, el, id, className & tagName?
Manual: http://documentcloud.github.com/backbone/#View-constructor

The code:

var myView = Backbone.View.extend({      //myVar : 'Hello from inside',      initialize: function() {         alert(this.myVar);     } )};  new myView({myVar: 'Hello from outside'}); 
like image 427
Industrial Avatar asked Dec 11 '11 11:12

Industrial


1 Answers

Options passed into the constructor are automatically stored as this.options

var myView = Backbone.View.extend({    myVar : 'Hello from inside',    initialize: function() {       alert(this.options.myVar);   } )};  new myView({myVar: 'Hello from outside'}); 
like image 121
czarchaic Avatar answered Oct 12 '22 05:10

czarchaic