问题
In java and C++ we could store a variable globally and access its value from any where in the project.
Say, i am inside a class called Residence
and i am saving the residenceNumber
which is a INT
to a global variable called houseNumberGlobalVariable
.
Now, i could access houseNumberGlobalVariable
from any class in the project. In a similar fashion, is there a Global variable in EXTJS
that i could use.
I need to set a value from one class and access it from another. What is the equivalent in EXT JS. I don't think there's a global variable concept in EXTJS, but what is the equivalent in it ?
回答1:
Just declare a variable in app.js
it will act as global variable.
var globalVar;
You can add anything into that like
globalVar.myStore = yourStore;
回答2:
Create a special class and put all your global
variables there.
Ext.define('MyApp.global.Vars', {
singleton: true,
....
houseNumberGlobalVariable: undefined
});
This way if you need access for it anywhere in the code just use MyApp.global.Vars.houseNumberGlobalVariable
回答3:
Based on your Application you can declare your global variable.
Scenario 1 : Declare inside the config
If you are going to use getter/setter methods (i.e change the variable frequently then go for config based)
Declaring the Appconstants
Ext.define('Practice.utilities.AppConstants', {
alias: 'widget.AppConstants',
config: {
mainVarTest: 'mainVarTest',
},
testvar: 'Testing value',
foo: 'bar',
meh: 42,
constructor: function(options) {
this.initConfig(options);
}
});
Calling the variable
var AppConstants = Ext.widget("AppConstants");
console.log(AppConstants.getMainVarTest());
Scenario 2 : Declare inside Singleton class
If your application needs global variable but it wont be alter any more inside the app. This class help to load the constant variable only once. (i.e you are not going changing the variable). This type is suits for your application
Declaring
Ext.define('Practice.utilities.AppConstants', {
alias: 'widget.AppConstants',
singleton: true,
testvar: 'Testing value',
foo: 'bar',
meh: 42,
});
Calling
var AppConstant=Practice.utilities.AppConstants;
console.log(AppConstant.foo);
Scenario 3 : Declare as Statics
Statics is static variable (exactly same as java). Advantage of using static variable is the life time of variable is indefinite longer period until explicitly cleared.
Ext.define("Practice.utilities.AppConstants", {
statics: {
mainVarTest: 'mainVarTest'
},
});
回答4:
I noticed the one thing missing here was a solution from the Sencha Architect program on how to create a global variable.
Go to your "Application" node on the project Inspector. Next click in the Configs search/Filter box, this is not only for searching but for creating config entries too. When you first look at this box it will have a Smokey pre-filled text of Filter Configs...
.
Enter your global variable name and you will notice nothing is found in the list below, (if the value is found in the config list then you already have this variable or your using a keyword). To the right of the text box and the lock icon is an "Add" button. Click the "Add" button to add your global variable into the application.
So lets say we have created the Foo
variable at this point. You will now see an entry in your config list. To the left of this entry is an ellipses button, this is currently designating that the new variable we created is a String. Click the button and change it to something different like a 'Number' field. Enter a value for this new variable and your now done, you have a global variable.
So how do you access this newly created variable? here is a sample bit of code stuffing the global into a local variable to be used.
var bar= MyApp.app.Foo;
The 'MyApp' reference is the Application name
config setting for the application you are creating. You can find this same value in the config settings of the Application node by filtering on name
.
回答5:
var globalVar = {}; add in your app.js file, its working fine
来源:https://stackoverflow.com/questions/11343469/is-there-global-variables-in-ext-js