ExtJS4:从Ext.Application访问全局变量

时间:2013-04-24 06:35:44

标签: extjs extjs4

我想加载一些特定于应用程序的设置,并在加载应用程序时将它们保存为全局变量。我找到了如何创建和访问全局变量here

这就是app.js的样子:

Ext.application({
    stores: [
        ...
    ],
    views: [
        ...
    ],
    autoCreateViewport: true,
    name: 'MyApp',
    controllers: [
        'DefaultController'
    ],

    launch: function() {
        ...
    }
});

是否可以在launch: function()块内设置这些变量?如果没有,还有其他选择吗?

1 个答案:

答案 0 :(得分:6)

您还可以创建单身人士:

Ext.define('MyApp.util.Utilities', {
     singleton: true,

     myGlobal: 1
});

在你的应用中:

Ext.application({
    stores: [
        ...
    ],
    views: [
        ...
    ],
    autoCreateViewport: true,
    name: 'MyApp',
    controllers: [
        'DefaultController'
    ],

    requires: ['MyApp.util.Utilities'], //Don't forget to require your class

    launch: function() {
        MyApp.util.Utilities.myGlobal = 2; //variables in singleton are auto static.
    }
});