javascript - Extjs 有一个协会

标签 javascript model-view-controller extjs model associations

我有 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})。 我需要使用什么 king of association,我试过 belongsTo,但是当我尝试使用 record.getMarital() 时,我得到一个错误“no such method on record”。

我使用 extjs 4

最佳答案

您应该使用 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
        }
    }
});

关于javascript - Extjs 有一个协会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11557444/

相关文章:

javascript - 从由 PHP echo 生成的表行单元格中的按钮执行 Javascript 函数

javascript - 将 touchstart 事件委托(delegate)给单击处理程序

extjs - 在 Extjs 中获取对 itemId 的引用

javascript - NPM 搜索远程包

javascript - 将鼠标悬停在隐藏的元素上以显示它

javascript - for (let key in data) 使用javascript循环1次或多次

javascript - Ember JS 过渡到嵌套路由,其中​​所有路由都是 View 中的动态段

java - PRPC中的controller是怎么写的

javascript - Modx 从特定选项卡隐藏内容字段

java - 如何在 GXT 3.x 中实现卡住列?