最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using global variables in backbone.js - Stack Overflow

programmeradmin0浏览0评论

So, first question I couldn't find an answer to. Might be reason enough to ask my own first question. Apologies if the answer can be found outside the scope of backbone.js.

In a backbone.js app, I need to have access to several variables in different functions, so I have to use some global variables setup.

I'm wondering if my current solution is acceptable/good practise. My IDE (IDEA) seems to think it isn't:

var MyModel = Backbone.Model.extend({

initialize:function(){
  var myGlobalVar, myOtherGlobalVar;//marked as unused local variable
},

myFunction:function() {          
      myGlobalVar = value;//marked as implicitly declared
      model.set({"mrJson": {"email": myGlobalVar}});
      model.save();
    });
  }
},

myOtherFunction:function() {          
      myOtherGlobalVar = otherValue;//marked as implicitly declared
      model.set({"mrJson": {"email": myGlobalVar, "other": myOtherGlobalVar}});
      model.save();
    });
  }
}
}

I tried declaring the implicitly declared globals, but that resulted in them not being accessible from the othe function.

Is there a proper way to do handle these global variables in backbone.js?

So, first question I couldn't find an answer to. Might be reason enough to ask my own first question. Apologies if the answer can be found outside the scope of backbone.js.

In a backbone.js app, I need to have access to several variables in different functions, so I have to use some global variables setup.

I'm wondering if my current solution is acceptable/good practise. My IDE (IDEA) seems to think it isn't:

var MyModel = Backbone.Model.extend({

initialize:function(){
  var myGlobalVar, myOtherGlobalVar;//marked as unused local variable
},

myFunction:function() {          
      myGlobalVar = value;//marked as implicitly declared
      model.set({"mrJson": {"email": myGlobalVar}});
      model.save();
    });
  }
},

myOtherFunction:function() {          
      myOtherGlobalVar = otherValue;//marked as implicitly declared
      model.set({"mrJson": {"email": myGlobalVar, "other": myOtherGlobalVar}});
      model.save();
    });
  }
}
}

I tried declaring the implicitly declared globals, but that resulted in them not being accessible from the othe function.

Is there a proper way to do handle these global variables in backbone.js?

Share Improve this question edited Feb 23, 2012 at 14:42 mu is too short 435k71 gold badges858 silver badges818 bronze badges asked Feb 23, 2012 at 11:13 SephieSephie 3311 gold badge3 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

The way you are currently declaring the variables, they are in the function initialize scope, rather than then object MyModel scope. To define the variables as Model variables (accessible to all object functions) do:

var MyModel = Backbone.Model.extend({

myGlobalVar: null,
myOtherGlobalVar: null,

initialize:function(){
  console.log(this.myGlobalVar)
},
...
发布评论

评论列表(0)

  1. 暂无评论