Home » Javascript » Backbone.js – passing arguments through constructors

Backbone.js – passing arguments through constructors

Posted by: admin April 3, 2018 Leave a comment

Questions:

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'});
Answers: