Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  3] [ 5]  / answers: 1 / hits: 17365  / 13 Years ago, thu, february 23, 2012, 12:00:00

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?


More From » backbone.js

 Answers
46

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)
},
...

[#87272] Wednesday, February 22, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
toddbrandtc

Total Points: 400
Total Questions: 104
Total Answers: 90

Location: Antigua and Barbuda
Member since Wed, Aug 4, 2021
3 Years ago
;