最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Extjs hasone association - Stack Overflow

programmeradmin4浏览0评论

I have Person model, it contains id, first_name, last_name etc and merital_id field. I also have Merital model contains only 2 field: id and title. Server responses JSON like:

{
    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}). 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".

I use extjs 4

I have Person model, it contains id, first_name, last_name etc and merital_id field. I also have Merital model contains only 2 field: id and title. Server responses JSON like:

{
    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}). 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".

I use extjs 4

Share Improve this question asked Jul 19, 2012 at 9:06 Luft-onLuft-on 1791 silver badge13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You should be using ExtJS Models, and Associations, in particular the HasOne association.

Documentation:

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

Example:

http://jsfiddle/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
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论