2013-04-02 15 views
0

比方说,我们有如果在执行Ext.application()之前需要他们的全局帮助器函数,我该如何放置它?

Ext.application({ 
    name: 'APP', 

    //this is my global helper method. 
    determineSomething: function() { 
     return true; 
    } 
}); 

,并应访问方法determineSomething()一个观点:

Ext.define('APP.view.Main', { 
extend: 'Ext.Panel', 

config: { 
    myConfigValue : APP.app.determineSomething() 
}}); 

这将工作,只要细如您使用煎茶类加载系统(因为JS这个Main-View的文件在APP可用后由Sencha加载)。

但现在让我们说我们想要缩小我们的脚本。这意味着浏览器在加载Sencha应用程序之前解析Main.js文件。但目前APP还没有准备好,我们无法访问它。

一种解决方案是简单地做一个非ext配置类/数组/无论与该方法,但这是一个很好的解决方案?什么是这个问题的干净解决方案?

回答

1

我会将我的帮助函数保存到一个单例Helper类中,该类在每次加载应用程序时都会被实例化。例如,这是我如何辅助类的样子:

Ext.define('MyntraTabApp.util.Helper', { 
    singleton : true, 
    alternateClassName : 'Helper', 
    determineSomething : function(){ 
     // do something 
    } 
}); 

我可以用它像这样的地方我想

Helper.determineSomething(); 
+0

请检查了这一点http://stackoverflow.com/questions/15343537/unable -to-access-namespace-app-variables-with-st2?answertab = active#tab-top如果使用全局变量/函数 – ThinkFloyd

+0

谢谢ThinkFloyd。我已经在http://stackoverflow.com/questions/15343537/unable-to-access-namespace-app-variables-with-st2?answertab=active#tab-top中解释了问题,你的帖子回答了问题做得好。 – Philipp

相关问题