我有一些json数据,格式如下:
{
"id":1,
"personMap":{
"president":{"firstname":"Sookie","lastname":"Stackhouse"},
"vicepresident":{"firstname":"Eric","lastname":"Northman"},
"treasurer":{"firstname":"Alcide","lastname":"Herveaux"}
}
}
如何定义模型以便使用正确的对象类型加载地图中的值?这可能吗?
以下内容将加载数据,但值不会输入为“Person”对象:
Ext.define('Committee', {
extend: 'Ext.data.Model',
fields: [ 'id', 'personMap' ]
});
以下期望personMap是一个数组而不是一个地图:
Ext.define('Committee', {
extend: 'Ext.data.Model',
fields: [ 'id' ],
hasMany: [{
model: 'Person',
name: 'personMap'
}]
});
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [ 'firstname', 'lastname' ]
});
答案 0 :(得分:2)
hasMany配置仅适用于数组。将数据结构更改为,
{
"id": 1,
"personMap": [
{
"firstname": "Sookie",
"lastname": "Stackhouse",
"title": "president"
},
{
"firstname": "Eric",
"lastname": "Northman",
"title": "vicepresident"
},
{
"firstname": "Alcide",
"lastname": "Herveaux",
"title": "treasurer"
}
]
}
会做的伎俩,但如果那不是一个选项,则无法使用hasMany配置。