【问题标题】:Extjs hasone associationExtjs hasone 关联
【发布时间】:2012-07-18 10:29:32
【问题描述】:

我有 Person 模型,它包含 id、first_name、last_name 等和 merital_id 字段。我也有 Merital 模型只包含 2 个字段:id 和 title。 服务器响应 JSON 如下:

{
    success: true,
    items: [
        {
            "id":"17",
            "last_name":"Smith",
            "first_name":"John",
            ...
            "marital_id":1,
            "marital": {
                "id":1,
                "title":"Female"
            }
        },
        ...
    ]
}

那么如何将我的模型与关联联系起来?我仍然可以在我的 column.renderer 中使用 record.raw.merital.title,但我不能在 {last_name} {first_name} ({merital.title}) 等模板中使用此类字段。 我需要使用什么关联之王,我尝试了 belongsTo,但是当我尝试使用 record.getMarital() 时出现错误“记录中没有此类方法”。

我使用 extjs 4

【问题讨论】:

    标签: javascript model-view-controller extjs model associations


    【解决方案1】:

    您应该使用 ExtJS 模型和关联,尤其是 HasOne 关联。

    文档:

    http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.association.HasOne

    例子:

    http://jsfiddle.net/el_chief/yrTVn/2/

    Ext.define('Person', {
        extend: 'Ext.data.Model',
        fields: [
            'id',
            'first_name',
            'last_name'
            ],
    
        hasOne: [
            {
            name: 'marital',
            model: 'Marital',
            associationKey: 'marital' // <- this is the same as what is in the JSON response
            }
        ],
    
        proxy: {
            type: 'ajax',
            url: 'whatever',
            reader: {
                type: 'json',
                root: 'items' // <- same as in the JSON response
            }
        }
    });
    

    【讨论】:

    • 好的,但是我使用 Sencha Architect,我在属性网格中看不到这种关联(
    • 嗯,但是每次我部署我的项目时它都会消失?
    • Sencha Architect 应该支持在线代码编辑。也许它还没有在这个更新中?
    • 在 Sencha Architect 中,我们可以使用 Association Config 关联不同的模型。当您单击“+”按钮时,它会询问您 Has ManyHas One 关联。之后我们可以选择模型、名称和关联键。
    猜你喜欢
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多