EXTJS hasone协会 [英] Extjs hasone association

查看:184
本文介绍了EXTJS hasone协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Person模型,它包含了ID,名字,姓氏等和 merital_id 字段。我也有Merital模型只包含2场:ID和标题。
服务器响应JSON如下:

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"
            }
        },
        ...
    ]
}

因此​​,如何能连接我的机型联想?我仍然可以在我的column.renderer使用record.raw.merital.title,但我不能在模板中使用这样的字段,如{姓氏} {} FIRST_NAME({} merital.title)。
协会的王什么,我需要用,我trye​​d的belongsTo,但是当我尝试使用record.getMarital()我得到一个错误在记载中没有这样的方法。

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".

我使用ExtJS的4

I use extjs 4

推荐答案

您应该使用ExtJS的模型和协会,特别是HasOne关联。

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

文件:

的http://文档.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
        }
    }
});

这篇关于EXTJS hasone协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆