2017-05-23 65 views
0

我习惯了MVC模式的ExtJs,并且我试图实现MVVM模式。我无法将商店绑定到我的视图。试图将存储绑定到ViewModel

我有一个主网格,并尝试在选择一条线时打开一个细节网格。

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

但对于实体店,我得到的错误:

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

我不明白,为什么到模型(model: 'rma.Detail')引用不会在视图模型的认可。使用MVC模式我不需要参考模型,我总是使用类似于rma.Details的引用来引用商店。

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

子公司的问题是:

  1. 这是设置在查看值id的正确方法。 ({xtype: 'rma-details', viewModel: {data: {id: id}}})?
  2. 我习惯在商店类中定义代理。这里我没有商店类,因为它是在ViewModel中定义的。像我上面那样在模型类中声明它是正确的吗?

回答

1

您需要定义schema,然后在模型声明中为其定义一个名称空间。或者,更好的是,在基本模型中(查看api文档中的模式摘要)。

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.

你试图在这里使用速记名字,但因为你还没有定义的架构命名空间,它不能把它解决的模型类。

子公司回应:

  1. 是的,你可以做到这一点。
  2. 在我看来,这里没有对错之分。 您可以在视图模型中声明代理旁边的过滤器。 您也可以在一个单独的类中声明商店,然后在视图模型中使用它(这是我使用的方法),此处仅指定绑定到某种视图模型数据的配置。
+0

您链接到的文档说:*默认情况下创建此类的单个实例*。为什么我必须定义一个模式呢?你的意思是在模型中添加'schema:{namespace:'Mb.model'}'是否应该解决这个问题? –

+0

是的,这应该可以解决问题。默认实例没有指定的命名空间配置。如果您不想声明架构,则可以在商店声明中指定模型的完整类名称。 – scebotari66

+0

在我的Viewmodel中,我用'store:'rma.Details''替换了'model:'rma.Detail'',现在我不再需要任何'schema'声明。但是现在我遇到了另一个问题:即使将rma.Details存储配置为remoteFilter:true, autoLoad:true,也不加载。 –