Skip to content Skip to sidebar Skip to footer

Backbone View Access Methods Variables

May be a simple questions... How do I access variables in backbone.js view? initialize: function() { //all init here } render: function() {//all render here } printFoo: function(e

Solution 1:

Short answer, you can't.

Reason: changeVar is a private member of changeFoo.

You could promote changeVar to become a member of the outer object. In which case, changeVar becomes accessible to initialize, render, printFoo and changeFoo.

function ConstructorFunctionName(){
    var changeVar = 'foo';
    /*this.changeVar = 'foo'; // this can also be used */

    this.initialize = function() { //all init here };
    this.render = function() { //all render here };

    this.printFoo = function(event) {
       var printVar = changeVar;
    };

    this.changeFoo = function(event) {
      this.changeVar = $(e.currentTarget).attr('id');
    };
}

http://jsfiddle.net/Njwdx/


Post a Comment for "Backbone View Access Methods Variables"