【问题标题】:ExtJs 6 stores config on Ext.app.Controller not workingExtJs 6 在 Ext.app.Controller 上存储配置不起作用
【发布时间】:2016-10-10 09:59:54
【问题描述】:

我刚刚注意到 Ext.app.Controller 上的 store config 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']
...
});

这将寻找

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

有人遇到过这个问题吗?

【问题讨论】:

  • 您是否require 控制器中的商店?这个例子表明它是必要的:Ext.app.Controller#cfg-stores.
  • 商店:['Menu'] 应该是你所需要的一切

标签: javascript extjs extjs6 extjs6-classic


【解决方案1】:

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

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 在 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;
        }
  }

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

【讨论】:

  • 并非所有实例都有它,而且如果我改用 storeId 来引用商店,我会丢失已经在使用的 getter,但感谢您的建议。
【解决方案2】:

我遇到过类似的问题,解决这个问题的方法是将我的应用程序需要的所有商店添加到 app/Application.js 中的 stores 配置中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 2012-03-02
  • 1970-01-01
  • 2018-02-04
  • 2023-03-17
相关资源
最近更新 更多