javascript EXT JS中是否有全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11343469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there Global variables in EXT JS
提问by sharon Hwk
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
.
在 java 和 C++ 中,我们可以全局存储变量并从项目中的任何位置访问它的值。说,我一个叫做类中Residence
,我保存residenceNumber
它是INT
一个叫做全局变量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.
现在,我可以houseNumberGlobalVariable
从项目中的任何类访问 。以类似的方式,是否有EXTJS
我可以使用的全局变量。
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 ?
我需要从一个类设置一个值并从另一个类访问它。EXT JS 中的等价物是什么?我认为 EXTJS 中没有全局变量的概念,但它的等价物是什么?
采纳答案by Jom
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;
回答by sha
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
回答by RED.Skull
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 (i.e 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. (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
场景 3:声明为静态
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.
Statics 是静态变量(与 java 完全相同)。使用静态变量的优点是变量的生命周期是无限长的,直到明确清除为止。
Ext.define("Practice.utilities.AppConstants", {
statics: {
mainVarTest: 'mainVarTest'
},
});
回答by Brian H
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. When you first look at this box it will have a Smokey pre-filled text of 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. 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.
所以假设我们Foo
在这一点上创建了变量。您现在将在配置列表中看到一个条目。此条目的左侧是一个省略号按钮,这当前表示我们创建的新变量是一个字符串。单击按钮并将其更改为不同的内容,例如“数字”字段。为这个新变量输入一个值,你现在完成了,你有一个全局变量。
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
.
“MyApp”引用是name
您正在创建的应用程序的应用程序配置设置。您可以通过过滤在应用程序节点的配置设置中找到相同的值name
。
回答by Raghulraj Palanisamy
var globalVar = {}; add in your app.js file, its working fine
var globalVar = {}; 添加你的 app.js 文件,它工作正常