我有 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/