javascript 在 ExtJS MVC 中在哪里添加全局变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865251/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:00:28  来源:igfitidea点击:

Where to add Global variables in ExtJS MVC?

javascriptextjsextjs4extjs-mvc

提问by Art F

I am wondering where to add global variables for an ExtJS Application. I already looked at some suggestions in stackoverflow that mention that you can add them inside app.js. But, can anyone be more specific? My app.js looks something like this:

我想知道在哪里为 ExtJS 应用程序添加全局变量。我已经查看了 stackoverflow 中的一些建议,其中提到您可以将它们添加到 app.js 中。但是,任何人都可以更具体吗?我的 app.js 看起来像这样:

Ext.application({

    launch: function() {..}

});

So, where exactly do the variables go? In the launch function? Outside Ext.application?

那么,变量究竟去了哪里?在启动功能中?外面Ext.application

采纳答案by Lloyd

Declare your own object namespace and add them there:

声明您自己的对象命名空间并将它们添加到那里:

Ext.ns('My.Application.Globals');

My.Application.Globals.SomeValue = 5;
My.Application.Globals.SomeText = 'Hello World!';

However globals are usually frowned upon unless absolutely needed, so try and get around using them if you can.

然而,除非绝对需要,否则全局变量通常是不受欢迎的,所以如果可以的话,尽量不要使用它们。

回答by rai.skumar

Better approach would be to create a separate class to hold such global constants. Then you should just put that constants class as requiresin app.js.

更好的方法是创建一个单独的类来保存这样的全局常量。然后你应该把这个常量类按要求放在app.js 中

Ext.define("App.Constants", {
         singleton  : true,   

         BASE_URL : "http://localhost:8080/",
         LABLE_HEADER : "geekrai.blogspot",
         TIMEOUT : 6000 
 });

This will ensure that class is loaded and now you can access any property/global value. I have mentioned the same in detail on my blog : link

这将确保类已加载,现在您可以访问任何属性/全局值。我在我的博客上详细提到了同样的内容:链接

回答by dbrin

I know you already accepted an answer which is fine. I just wanted to add an MVC way to include namespaced variables available to the app. There is one caveat to these 'globals' - you can not use them in your class definitions. Meaning you can not reference your app in Ext.define({})methods. They have to be use in initComponentmethod or later.

我知道你已经接受了一个很好的答案。我只是想添加一种 MVC 方式来包含应用程序可用的命名空间变量。这些“全局变量”有一个警告 - 您不能在类定义中使用它们。这意味着您不能在Ext.define({})方法中引用您的应用程序。它们必须在initComponent方法中或以后使用。

So here is what I do:

所以这就是我要做的:

Ext.application({
    name:'MyApp',
    appFolder:'js/app',
    controllers:[ 'Main' ],
    autoCreateViewport : true,
    launch:function () {
        console.log("App Launched!");
        MyApp.app = this;   //added this to get reference to app instance. IMPORTANT!    
    }, 
    //variables used throughout the app
    globals:{
        myURL:'http://example.com',
        magicNum:5
    }
});

To use these application wide variables you reference your app namespace and so do not pollute the global space. Like this:

要使用这些应用程序范围的变量,您需要引用您的应用程序命名空间,因此不要污染全局空间。像这样:

MyApp.app.gloabals.magicNum

回答by Adam Rackis

Aside from whatever features may be built into Ext, you can always use an immediate function to create a closure:

除了可能内置的任何功能外Ext,您始终可以使用立即函数来创建闭包:

(function(){
    var globalVariable = 'foo';    

    Ext.application({

        launch: function() { alert(globalVariable); }

    });

})();