ExtJs 6存储Ext.app.Controller上的配置不起作用

Ryu*_*yuk 5 javascript extjs extjs6 extjs6-classic

我刚注意到Ext.app.Controller上的商店配置http://docs.sencha.com/extjs/6.0/6.0.2-classic/#!/api/Ext.app.Controller-cfg-stores没有查找正确的路径(与视图配置相同).

例如

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

这将寻找

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这样的完整类名,但是getter会非常难看.(这是在我刚刚升级的庞大代码库上发生的,因此使用完整的类名称将是我的最后一个资源).

有人遇到过这个问题吗?

Ryu*_*yuk 2

我已经找到原因了(请耐心等待):

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

看着:

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

这些是重要的,一旦命名空间被解析,就会调用进程依赖项:

Controller.processDependencies(proto, requires, namespace, 'store', data.stores);
Run Code Online (Sandbox Code Playgroud)

我对此进行了研究,发现 Ext.app.getNamespace 在 ext 5 和 6 中是相同的

那么为什么它出现在 ExtJs 5 中

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

在 ExtJs 6 上

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

通过console.log发现原因,Ext.ClassManager.paths现在有一个新条目对应于MyApp.controller

在此输入图像描述

以前没有 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;
        }
  }
Run Code Online (Sandbox Code Playgroud)

如果您知道更好的解决方案请告诉我!