2013-04-24 118 views
3

我想加載一些特定於應用程序的設置,並在應用程序加載時將它們保存爲全局變量。我已經找到了如何創建和訪問全局變量hereExtJS4:從Ext.Application訪問全局變量

這是我app.js看起來像:

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

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

是否有可能設置裏面launch: function()塊這些變量?如果不是,有沒有其他的選擇?

回答

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

我只是發現了,你張貼在時間:) 此外,它的工作原理沒有'singleton'太。謝謝! – vahissan 2013-04-24 08:58:36

+3

是的,但是如果你'Ext.ceate()'有'singleton:true'的類,你將不會有新的實例,所以使用它更安全。 – 2013-04-24 09:07:56

+0

爲什麼我無法在商店中訪問myGlobal? – 2017-08-08 06:48:14