尝试将商店绑定到ViewModel [英] Trying to bind a store to a ViewModel

查看:106
本文介绍了尝试将商店绑定到ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了使用MVC模式的ExtJ,并且正在尝试实现MVVM模式.我无法将商店绑定到我的视图.

I'm used to ExtJs with MVC pattern, and I'm trying to implement MVVM pattern. I'm not able to bind a store to my view.

我有一个主网格,并在选择一条线时尝试打开一个详细信息网格.

I have a main grid, and try to open a details grid when selecting a line.

detailsView = mainPanel.add({
   xtype: 'rma-details',
   viewModel: {data: {id: id}}
})

Ext.define('Mb.view.rma.Details', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.rma-details',
    requires: [
        'Mb.view.rma.DetailsController',
        'Mb.view.rma.DetailsModel'
    ],
    controller: 'rma-details',
    viewModel: {type: 'rma-details'},
    bind: {
        title: 'Retour n° {id}',
        store: '{details}'
    },
    (...)
});

Ext.define('Mb.view.rma.DetailsModel', {
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.rma-details',
    requires: ['Mb.model.rma.Detail'],
    data: {
        id: 0
    },
    stores:{
        details: {
            model: 'rma.Detail',
            filters: [{
                property: 'rma',
                value: '{id}'
            }]
        }
    }
});

Ext.define('Mb.model.rma.Detail', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id', type: 'int'},
        {name: 'rma', type: 'int'},
        (...)
    ],
    proxy: { // cf. 2nd subsidiary question
        (...)
    }
});

视图的标题已正确绑定到id的值.

The title of the view gets bound correctly to the value of id.

但是对于商店,我会收到错误消息:

But for the store, I get the errors:

[E] Ext.data.schema.Schema.lookupEntity():没有这样的实体"rma.Detail".
未捕获的错误:没有这样的实体"rma.Detail".

[E] Ext.data.schema.Schema.lookupEntity(): No such Entity "rma.Detail".
Uncaught Error: No such Entity "rma.Detail".

我不明白为什么ViewModel中无法识别对模型(model: 'rma.Detail')的引用.使用我不需要引用模型的MVC模式,我总是使用类似于rma.Details的引用来引用商店.

I don't understand why the reference to the model (model: 'rma.Detail') is not recognized in the ViewModel. Using the MVC pattern I never needed to reference the model, I always referenced the store using a reference similar to rma.Details.

主要问题是:如何在ViewModel中声明模型rma.Details?

The main question is: How do I need to declare the model rma.Details in the ViewModel ?

子问题是:

  1. 这是在视图中设置值id的正确方法. ({xtype: 'rma-details', viewModel: {data: {id: id}}})吗?
  2. 我习惯总是在商店类中定义代理.在这里,我不再有商店类,因为它是在ViewModel中定义的.像我在上面一样在模型类中声明它是否正确?
  1. Is this the correct way to set the value id in the View. ({xtype: 'rma-details', viewModel: {data: {id: id}}}) ?
  2. I'm used to define the proxy always in the store class. Here I don't have a store class anymore, because it is defined in the ViewModel. Is it correct to declare it in the model class like I did it above ?

推荐答案

您需要定义

You need to define a schema, and then a namespace for it in the model declaration. Or, better yet, in a base model (check out the summary for schemas from the api docs).

在描述实体之间的关联时,希望使用不包含公共名称空间部分的速记名称.这被称为entityName,而不是其类名.默认情况下,entityName是完整的类名.但是,如果使用命名空间,则可以丢弃公共部分,并且可以派生一个较短的名称.

When describing associations between entities, it is desirable to use shorthand names that do not contain the common namespace portion. This is called the entityName as opposed to its class name. By default, the entityName is the full class name. However, if a namespace is used, the common portion can be discarded and we can derive a shorter name.

您尝试在此处使用简写名称,但由于尚未定义架构名称空间,因此无法将其解析为模型类.

You've tried to use a shorthand name here, but because you haven't defined a schema namespace, it can't solve it to the model class.

辅助回复:

  1. 是的,您可以这样做.
  2. 我认为在这里没有对与错. 您可以在视图模型中在过滤器旁边声明代理. 您还可以在一个单独的类中声明存储,然后在viewmodel中使用它(这是我使用的方法),这里仅指定绑定到某种viewmodel数据的配置.
  1. Yes, you can do this.
  2. There is no right or wrong here, in my opinion. You can declare the proxy alongside the filters, in the view model. You can also declare the store in a separate class and then use it in the viewmodel (this is the approach I use), specifying here only the configs that are bound to some kind of viewmodel data.

这篇关于尝试将商店绑定到ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆