2014-02-06 23 views
7

我有一個ExtJS單例類。爲什麼我的ExtJS單身人士不工作?

作爲一個測試,我在app.js launch()函數中調用它的方法。

但是沒有定義單例靜態方法。

我想當我要求班級單身人士變得活躍?

Ext.Loader.setConfig({ 
    enabled : true, 
    paths: { 
     'AM': 'app' 
    } 
}); 


Ext.application({ 
    name: 'AM', 
    autoCreateViewport: true, 


    requires: [ 
     'AM.localization.ResourceManager' 
    ], 


    controllers: [ 
     'Users' 
    ], 


    launch: function() { 
     alert(ResourceManager.initBundleLoader()); 
    } 
}); 




Ext.define('AM.localization.ResourceManager', { 
    alternateClassName: 'ResourceManager', 
    singleton: true, 

    init: function() { 
     this.initBundleLoader(); 
    }, 

    statics: { 
     test: 'here', 
     initBundleLoader: function() { 
      debugger; 
      Ext.applyIf(Ext.Loader, { 
       resourceBundles: new Object() 
      }); 
     }, 

     registerBundle: function(bundleName, locale) { 
      debugger; 
      if(!Ext.Loader.hasOwnProperty('resourceBundles')) { 
       this.initBundleLoader(); 
      } 
      if(!Ext.Loader.resourceBundles.hasOwnProperty(bundleName)) { 
       if(Ext.ClassManager.isCreated('AM.locale.' + locale + '.resources.' + bundleName)) { 
        this.resourceBundles.bundleName = Ext.create('AM.locale.' + locale + '.resources.' + bundleName); 
       } 
      } 
     } 
    } 
}); 

回答

8

在Ext JS中,你可以定義類爲singleton或定義與statics方法正常類。你不能在單例中定義靜態方法。

如果您將類定義爲singleton Ext JS類後處理器會立即創建該類的一個實例,並且在您的案例中將參考存儲在AM.localization.ResourceManager中。然後,您可以訪問諸如AM.localization.ResourceManager.initBundleLoader()

與靜態方法和單正常類之間差異的良好explenation你可以在第三個帖子在這裏找到單方法:http://www.sencha.com/forum/showthread.php?128646-Singleton-vs-class-with-all-static-members

所以,你的類定義應該是:

Ext.define('AM.localization.ResourceManager', { 
    alternateClassName: 'ResourceManager', 
    singleton: true, 

    init: function() { 
     this.initBundleLoader(); 
    }, 

    test: 'here', 
    initBundleLoader: function() { 
     debugger; 
     Ext.applyIf(Ext.Loader, { 
      resourceBundles: new Object() 
     }); 
    }, 

    registerBundle: function(bundleName, locale) { 
     debugger; 
     if(!Ext.Loader.hasOwnProperty('resourceBundles')) { 
      this.initBundleLoader(); 
     } 
     if(!Ext.Loader.resourceBundles.hasOwnProperty(bundleName)) { 
      if(Ext.ClassManager.isCreated('AM.locale.' + locale + '.resources.' + bundleName)) { 
       this.resourceBundles.bundleName = Ext.create('AM.locale.' + locale + '.resources.' + bundleName); 
      } 
     } 
    } 
}); 
+0

現在效果很好。謝謝你的幫助! –

相關問題