2016-06-09 150 views
5

我剛剛注意到,Ext.app.Controller上的商店配置http://docs.sencha.com/extjs/6.0/6.0.2-classic/#!/api/Ext.app.Controller-cfg-stores沒有看到 在正確的路徑(發生與視圖配置相同)。ExtJs 6商店配置Ext.app.Controller不工作

e.g

Ext.define('MyApp.controller.Menu', { 
    extend: 'Ext.app.Controller', 
    stores: ['Menu'] 
... 
}); 

這將尋找

http://localhost/myapp/app/controller/store/Menu.js?_dc=20160607211025

通知控制器文件夾

,而不是

http://localhost/myapp/app/store/Menu.js?_dc=20160607211025

當時我以爲這是專門針對我的一個項目的配置問題,但隨後得到了在不同的項目同樣的事情的開始。

我使用ExtJS的6.02

我知道我可以使用完整的類名像MyApp.store.Menu但隨後的吸氣劑會非常難看。 (這發生在我剛剛升級的龐大代碼庫上,因此使用完整的類名稱將是我的最後一個資源)。

有人遇到過這個問題嗎?

+0

您是否需要控制器中的商店?這個例子表明它是必要的:[Ext.app.Controller#cfg-stores](http://docs.sencha.com/extjs/6.0.2-classic/Ext.app.Controller.html#cfg-stores)。 –

+0

商店:['Menu'] 應該是你所需要的所有 –

回答

2

我已經找到了原因(裸與我):

https://docs.sencha.com/extjs/6.0/6.0.2-classic/source/Controller2.html#Ext-app-Controller

看看:

onClassExtended - > Controller.resolveNamespace - > Ext.app.getNamespace

這些是重要的,一旦命名空間解決了就有一個調用來處理依賴關係:

Controller.processDependencies(proto, requires, namespace, 'store', data.stores); 

我研究這一點,並Ext.app.getNamespace是在分機5和6

所以爲什麼它在ExtJs的5

Ext.getNamespace("MyApp.controller.SomeController"); // returns MyApp

和ExtJs的6個相同

Ext.getNamespace("MyApp.controller.SomeController"); // returns MyApp.controller

原因是由控制檯發現的。登錄Ext.ClassManager.paths現在有相當於MyApp.controller

enter image description here

一個新條目以前有對MyApp.controller(ZHT.controller)無鑰匙

而且確實是尋找什麼Ext.getNameSpace爲'最深的前綴」,你可以在這裏看到http://docs.sencha.com/extjs/6.0/6.0.2-classic/source/Util.html#Ext-app-Util

[更新] 這麼一兩件事是可以做的是重寫靜態方法resolveNamespace像這樣:

statics: { 
    resolveNamespace: function(cls, data) { 
      var Controller = Ext.app.Controller, 
       namespaceRe = cls.prototype.isProfile ? Controller.profileRegex : Controller.controllerRegex, 
       className, namespace, match; 
      /* 
      * Namespace resolution is tricky business: we should know what namespace 
      * this Controller descendant belongs to, or model/store/view dependency 
      * resolution will be either ambiguous or plainly not possible. To avoid 
      * guessing games we try to look for a forward hint ($namespace) that 
      * Application class sets when its onClassExtended gets processed; if that 
      * fails we try to deduce namespace from class name. 
      * 
      * Note that for Ext.app.Application, Controller.onClassExtended gets executed 
      * *before* Application.onClassExtended so we have to delay namespace handling 
      * until after Application.onClassExtended kicks in, hence it is done in this hook. 
      */ 
      className = Ext.getClassName(cls); 
      namespace = data.$namespace || data.namespace || 
       Ext.app.getNamespace(className) || 
       ((match = namespaceRe.exec(className)) && match[1]); 

      //<debug> 
      if (!namespace) { 
       Ext.log.warn("Missing namespace for " + className + ", please define it "+ 
        "in namespaces property of your Application class."); 
      } 
      //</debug> 


      //This is the only change on this override. 
      //http://stackoverflow.com/questions/37731213/extjs-6-stores-config-on-ext-app-controller-not-working/37733261#37733261 
      if(namespace && namespace.indexOf(".controller") > -1) { 
       namespace = namespace.slice(0, namespace.indexOf(".controller")); 
      } 

      return namespace; 
     } 
    } 

如果您知道更好的解決方案,請告訴我!

+0

商店是否有'storeId'?這可以幫助.. –

+0

不是所有的實例都有它,如果我切換到使用storeId來引用商店,我將失去已被使用的getters,但是感謝您的建議。 –

0

我遇到過類似的問題,解決這個問題的方法是將我的應用程序需要的所有商店添加到app/Application.jsstores配置中。

+0

即使我需要Application.js上的商店,我仍然需要將商店配置保存在控制器中,以便我可以使用自動執行的相應getter。 http://docs.sencha.com/extjs/6.0/6.0.2-classic/#!/api/Ext.app.Controller-cfg-stores –