2014-10-28 40 views
6

我想了解Extjs 5數據模型中的新關聯概念。Extjs 5數據模型 - 有很多關聯

我有以下型號

// User 
Ext.define('App.model.User', { 
    extend: 'App.model.Base',  
    fields: [ 
     {name: 'id', type: 'string'}, 
     {name: 'name', type: 'string'}, 
    ],  
    manyToMany: { 
     Categories: { 
      type: 'Categories', 
      role: 'categories', 
      field: 'categories', 
      right: true 
     } 
    } 
}); 

// Category 
Ext.define('App.model.Category', { 
    extend: 'App.model.Base',  
    constructor: function() {...}, 
    fields: [ 
     {name: 'id', type: 'string'}, 
     {name: 'categoryName', type: 'string'}, 
    ] 
}); 

我已經得到了用戶的以下JSON:

{ "user": { "id": "1", "name": "Foo", "categories": [1, 2, 3] } } 

User模型加載它應該初始化類商店與數據。

(我Category模型知道到數字轉換爲ID &類別名稱的對象)

出於某種原因,當我嘗試獲取用戶的類別商店是空的。

userRecord.categories(); // has no records 

我該如何得到這個工作?

+0

你真的想要一個的M-M?它應該從哪裏得到類別? – 2014-10-30 19:55:05

+0

@EvanTrimboli:我真的需要它被映射成一種方式,並且我有一個硬編碼的分類地圖,所以我可以將該ID轉換爲它的名稱。我在這裏錯過了什麼? – guess 2014-11-01 18:40:22

回答

1

請您嘗試一下

//用戶

Ext.define('User', { 
    extend: 'app.data.Model',  
    fields: [ 
     {name: 'id', type: 'string'}, 
     {name: 'name', type: 'string'}, 
    ],  
    hasMany: { 
     Categories: { 
      type: 'Categories', 
      role: 'categories', 
      field: 'categories', 
      right: true 
     } 
    } 
});