问题描述
我有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)。
协会的王什么,我需要用,我tryed的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.
文件:
例如:
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协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!