When considering your Application, you have the ability to define global variables.
Option 1: Define within the configuration
If you plan on using getter/setter methods (i.e. frequently changing the variable), it is recommended to use a configuration-based approach.
Defining 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);
}
});
Accessing the variable:
var AppConstants = Ext.widget("AppConstants");
console.log(AppConstants.getMainVarTest());
Option 2: Define within a Singleton class
If your application requires a global variable that will not be altered within the app, consider using a Singleton class to load the constant variable only once. This approach is suitable when the variable does not need to be changed.
Defining:
Ext.define('Practice.utilities.AppConstants', {
alias: 'widget.AppConstants',
singleton: true,
testvar: 'Testing value',
foo: 'bar',
meh: 42,
});
Accessing:
var AppConstant=Practice.utilities.AppConstants;
console.log(AppConstant.foo);
Option 3: Define as Statics
Statics refer to static variables (similar to Java). Using static variables ensures that the variable's lifetime extends for an indefinite period until explicitly cleared.
Ext.define("Practice.utilities.AppConstants", {
statics: {
mainVarTest: 'mainVarTest'
},
});