1

可能是一个简单的问题......如何访问backbone.js 视图中的变量?

initialize: function() { //all init here }
render: function() {//all render here }

printFoo: function(event) {
   var printVar = this.changeFoo.changeVar  // how do I access changeVar  here???
}

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

如何访问 changeVar ?

4

1 回答 1

3

简短的回答,你不能。

原因:changeVarchangeFoo.

您可以提升 changeVar成为外部对象的成员。在这种情况下,、和可以changeVar访问。initializerenderprintFoochangeFoo

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/

于 2012-09-12T15:25:33.153 回答