骨干视图访问方法的变量(backbone view access methods variables

2019-10-17 05:27发布

可能是一个简单的问题...我如何访问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?

Answer 1:

简短的回答,你不能。

原因: changeVar是的私有成员changeFoo

你可以促进 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/



文章来源: backbone view access methods variables