【问题标题】:Extjs 5 data model - has many associationExtjs 5 数据模型 - 有很多关联
【发布时间】:2014-10-28 11:24:38
【问题描述】:

我正在尝试理解 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 & categoryName 的对象)

由于某种原因,当我尝试获取用户的类别时,商店是空的。

userRecord.categories(); // has no records

我怎样才能让它工作?

【问题讨论】:

  • 你真的想要一个 m-m 吗?它应该从哪里获取类别?
  • @EvanTrimboli:我真的需要以一种方式映射它,并且我有一个硬编码的类别映射,所以我可以将 Id 转换为它的名称。我在这里错过了什么?

标签: extjs extjs5


【解决方案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
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 2018-05-17
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多