ExtJS4: Accessing global variables from Ext.Application

杀马特。学长 韩版系。学妹 提交于 2019-12-21 20:33:05

问题


I want to load some application specific settings and save them as global variables when the application is loaded. I have found how to create and access global variable here.

This is how my app.js looks like:

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

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

Is it possible to set these variables inside launch: function() block? If not, are there any alternatives?


回答1:


You can also create a singleton:

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

     myGlobal: 1
});

in your app:

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


来源:https://stackoverflow.com/questions/16184981/extjs4-accessing-global-variables-from-ext-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!