Are there global variables in EXT JS

In java and C ++, we can store a variable globally and access its value from anywhere in the project. Let's say I'm inside a class called Residence , and I save residenceNumber , which is an INT global variable called houseNumberGlobalVariable .

Now I could access houseNumberGlobalVariable from any project class. Similarly, there is a global variable in EXTJS that I could use.

I need to set a value from one class and access it from another. Which is equivalent in EXT JS. I don’t think there is a global variable in EXTJS, but what is equivalent to it?

+6
source share
4 answers

just declare a variable in app.js, it will act as a global variable.

 var globalVar; 

You can add something to this as

 globalVar.myStore=yourStore; 
+3
source

Create a special class and put all your global variables there.

 Ext.define('MyApp.global.Vars', { singleton: true, .... houseNumberGlobalVariable: undefined }); 

Thus, if you need access for it anywhere in the code, just use MyApp.global.Vars.houseNumberGlobalVariable

+15
source

Based on your application, you can declare your global variable.

Scenario 1: declare inside config

If you intend to use getter / setter methods (for example, change the variable often, then go to the configuration)

Announcement 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); } }); 

Variable call

 var AppConstants = Ext.widget("AppConstants"); console.log(AppConstants.getMainVarTest()); 

Scenario 2: declare inside a Singleton class

If your application needs a global variable, but it will no longer change inside the application. This class helps to load a constant variable only once. (i.e. you are not going to change the variable). This type is suitable for your application.

Ad

  Ext.define('Practice.utilities.AppConstants', { alias:'widget.AppConstants', singleton : true, testvar: 'Testing value', foo: 'bar', meh: 42, }); 

Call

 var AppConstant=Practice.utilities.AppConstants; console.log(AppConstant.foo); 

Scenario 3: declare as static

Statics is a static variable (exactly the same as java). The advantage of using a static variable is the lifetime of the variable - an indefinite longer period until it is explicitly deleted.

 Ext.define("Practice.utilities.AppConstants",{ statics : { mainVarTest : 'mainVarTest' }, }); 
+2
source

I noticed that one of the missing here was Sencha Architect's decision on how to create a global variable.

Go to your “Application” node in the project inspector. Then click on the “Search / Filter Settings” window, this is not only for searching, but also for creating configuration entries. When you first look at this window, it will have the pre-populated Smokey Filter Configs... text.

Enter the name of the global variable, and you will notice that nothing is found in the list below (if the value is found in the configuration list, then you already have this variable or you are using the keyword) . To the right of the text box and the lock icon is the Add button. Click the Add button to add the global variable to the application.

So, let's say we created the variable Foo at this point. You will now see an entry in your configuration list. To the left of this entry is a button with ellipses, currently this means that the new variable that we created is a string. Click the button and change it to something else, like the Number field. Enter a value for this new variable, and now you have made a global variable.

So how do you access this newly created variable? here is an example bit of code that stuffs global into a local variable to be used.

 var bar= MyApp.app.Foo; 

The "MyApp" link is the name application configuration parameter for the application being created. This same value can be found in the node application configuration settings by filtering on name .

+1
source

Source: https://habr.com/ru/post/919748/


All Articles