简体   繁体   English

Extjs有一个联想

[英]Extjs hasone association

I have Person model, it contains id, first_name, last_name etc and merital_id field. 我有Person模型,它包含id,first_name,last_name等和merital_id字段。 I also have Merital model contains only 2 field: id and title. 我也有Merital模型只包含2个字段:id和title。 Server responses JSON like: 服务器响应JSON如:

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

So how can I connect my models with association? 那么如何将我的模型与关联联系起来呢? I still can use record.raw.merital.title in my column.renderer, but I cannot use such fields in templates like {last_name} {first_name} ({merital.title}). 我仍然可以在我的column.renderer中使用record.raw.merital.title,但我不能在像{last_name} {first_name}({merital.title})这样的模板中使用这些字段。 What king of association do I need to use, I tryed belongsTo, but when I try to use record.getMarital() I get an error "no such method on record". 我需要使用什么样的关联王,我尝试使用belongsTo,但是当我尝试使用record.getMarital()时,我得到一个错误“记录中没有这样的方法”。

I use extjs 4 我使用extjs 4

You should be using ExtJS Models, and Associations, in particular the HasOne association. 您应该使用ExtJS模型和关联,特别是HasOne关联。

Documentation: 文档:

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

Example: 例:

http://jsfiddle.net/el_chief/yrTVn/2/ 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
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM