2017-05-23 64 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,也不加載。 –