【问题标题】:ExtJS4: Accessing global variables from Ext.ApplicationExtJS4:从 Ext.Application 访问全局变量
【发布时间】:2013-04-17 14:10:11
【问题描述】:

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

这就是我的app.js 的样子:

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

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

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

【问题讨论】:

    标签: extjs extjs4


    【解决方案1】:

    你也可以创建一个单例:

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

    【讨论】:

    • 我刚刚发现并及时发布了它:) 此外,它也可以在没有singleton 的情况下使用。谢谢!
    • 是的,但是如果你 Ext.ceate() 一个带有 singleton: true 的类,你将不会有一个新的实例,所以使用它会更安全。
    • 为什么我无法在商店中访问 myGlobal?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2011-10-29
    • 2014-02-15
    • 1970-01-01
    相关资源
    最近更新 更多