【问题标题】:Viewmodel attribute 'links' cannot find the model typeViewmodel 属性“链接”找不到模型类型
【发布时间】:2016-02-08 04:34:22
【问题描述】:

在尝试在show() 上执行以下代码时,我们得到一个异常,即links 属性无法找到模型,无论它是由类指定还是由实体名称指定。

Ext.define('myapp.view.film.FilmsViewController', {
//extend: 'myapp.view.base.ViewController',
extend: 'Ext.app.ViewController',

alias: 'controller.films',

onAdd: function(button, event, options) {
    this.createDialog(null)
},

createDialog: function(record) {
    var me = this;
    var view = me.getView();    //here is film panel

    me.isEdit = !!record;   //convert record to boolean

    me.dialog = view.add({ //#3
        xtype: 'filmwindow',
        viewModel: { //#4
            data: { //#5
                title: record ? 'Edit: ' + record.get('title') : 'Add New Film',
            },
            links: { //#6
                currentFilm: record || { //#7
                    //type: 'Film',
                    type: 'myapp.model.film.Film',
                    create: true
                }
            }
        },
        //session: true
    });
    me.dialog.show();
},

如果我们注释代码的links 部分,其余的工作正常。

这里是异常的有趣部分:

[E] Ext.app.ViewModel.getRecord(): Invalid model name: myapp.model.film.Film
log @ ext-all-rtl-debug.js?_dc=1446847440066:9121
Ext.apply.raise @ ext-all-rtl-debug.js?_dc=1446847440066:2606
Ext.raise @ ext-all-rtl-debug.js?_dc=1446847440066:2691
Ext.define.privates.getRecord @ ext-all-rtl-debug.js?_dc=1446847440066:99865
Ext.define.linkTo @ ext-all-rtl-debug.js?_dc=1446847440066:99748
Ext.define.privates.applyLinks @ ext-all-rtl-debug.js?_dc=1446847440066:100120

如果您深入研究源代码,您会发现检查 myapp.model.film.Film 是否为类的 if 语句失败..

【问题讨论】:

  • 尝试添加 requires: [ 'myapp.model.film.Film' ] 作为视图控制器的属性 - 听起来该类尚未在任何地方指定为依赖项,因此不会在编译源中包含/解析。
  • @Emissary 感谢您的帮助。这也是我们最初的想法,但最终的解决方案来自一个更疯狂的地方。在下面找到它

标签: javascript extjs mvvm extjs6 dynamic-class-creation


【解决方案1】:

花了一整天的时间并运用我们最疯狂的想象力,我们设法弄清楚发生了什么:

首先检查这个链接:https://www.sencha.com/forum/showthread.php?299699-Any-use-of-a-model-schema-breaks-Tree-model-even-if-not-extending.&p=1118964&viewfull=1#post1118964

您会发现,如果您在源代码中无缘无故地使用多个架构,这些架构会相互冲突,您必须提供唯一架构 ID

现在这个自定义配置应该传播到所有其他配置,这意味着除非您指定要使用的架构 ID,否则 ViewModel 将无法工作。

换句话说,视图模型只有在您添加这样的架构时才能工作:

viewModel: {
        schema: "youruniqueschemaid",

        data: {
            title: record ? 'Edit: ' + record.get('title') : 'Add New Film',
        },
        links: {
            currentFilm: record || {
                //type: 'Film',
                type: 'myapp.model.film.Film',
                create: true
            }
        }
    }

是的,links 中的 type 属性具有误导性!

如果您已将模型内的entityName 属性设置为Film,您也可以使用更短的版本type: "Film"

现在重构

相反,Sencha 应该做的是强制所有开发人员在 ViewModel 中显式设置模式,如果模型没有使用模式设置,则使用 null。

当然,正如您所理解的那样,解决此类问题无法通过深入研究文档或深入研究源代码来完成,而是通过疯狂猜测使用了哪种疯狂的约定。

一般来说,框架应该更明确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 2018-07-09
    • 2017-12-16
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多