简体   繁体   English

EXT JS中是否有全局变量

[英]Is there Global variables in EXT JS

In java and C++ we could store a variable globally and access its value from any where in the project. 在java和C ++中,我们可以全局存储一个变量,并从项目的任何位置访问它的值。 Say, i am inside a class called Residence and i am saving the residenceNumber which is a INT to a global variable called houseNumberGlobalVariable . 比如说,我在一个名为Residence的类中,我将residenceNumber保存为一个名为houseNumberGlobalVariable的全局变量的INT

Now, i could access houseNumberGlobalVariable from any class in the project. 现在,我可以从项目中的任何类访问houseNumberGlobalVariable In a similar fashion, is there a Global variable in EXTJS that i could use. 以类似的方式,我可以使用EXTJS中的全局变量。

I need to set a value from one class and access it from another. 我需要从一个类中设置一个值,然后从另一个类中访问它。 What is the equivalent in EXT JS. EXT JS中的等价物是什么。 I don't think there's a global variable concept in EXTJS, but what is the equivalent in it ? 我不认为EXTJS中有一个全局变量概念,但它的等价物是什么?

Create a special class and put all your global variables there. 创建一个特殊类并将所有global变量放在那里。

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 这样,如果您需要在代码中的任何位置访问它,只需使用MyApp.global.Vars.houseNumberGlobalVariable

Just declare a variable in app.js it will act as global variable. 只需在app.js声明一个变量,它就会充当全局变量。

var globalVar;

You can add anything into that like 你可以添加任何东西

globalVar.myStore = yourStore;

Based on your Application you can declare your global variable. 根据您的应用程序,您可以声明您的全局变量。

Scenario 1 : Declare inside the config 场景1:在配置中声明

If you are going to use getter/setter methods (ie change the variable frequently then go for config based) 如果你打算使用getter / setter方法(即频繁更改变量,那么请选择基于配置)

Declaring the Appconstants 声明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 场景2:在Singleton类中声明

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. 此类仅帮助加载常量变量一次。 (ie 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 场景3:声明为静态

Statics is static variable (exactly same as java). 静态变量是静态变量(与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'
     },
 });

I noticed the one thing missing here was a solution from the Sencha Architect program on how to create a global variable. 我注意到这里缺少的一件事是Sencha Architect程序中关于如何创建全局变量的解决方案。

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. 接下来单击Configs搜索/过滤器框,这不仅用于搜索,还用于创建配置条目。 When you first look at this box it will have a Smokey pre-filled text of Filter Configs... . 当你第一次看到这个盒子时,它将有一个Filter Configs...的Smokey预填充文本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. 所以我们假设我们已经创建了Foo变量。 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. “MyApp”参考是您正在创建的应用程序的应用程序name配置设置。 You can find this same value in the config settings of the Application node by filtering on name . 您可以通过过滤name在“应用程序”节点的配置设置中找到相同的值。

var globalVar = {}; var globalVar = {}; add in your app.js file, its working fine 添加你的app.js文件,它的工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM