【发布时间】:2021-04-01 01:04:51
【问题描述】:
我们正在创建一个采用 JSON:API 规范 (https://jsonapi.org/) 的 API (v2) 新版本。我无法将 ExtJS 模型关联 (belongs_to) 移植到新模式。
ExtJS 文档只展示了如何在同一个根节点 (https://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.association.Association) 中使用嵌套关系。
v1 数据(样本):
{
"data": [
{
"id": 1,
"description": "Software Development",
"area_id": 1,
"area": {
"id": 1,
"code": "01",
"description": "Headquarters"
}
},
],
"meta": {
"success": true,
"count": 1
}
}
v2 数据(样本):
{
"data": [
{
"id": "1",
"type": "maint_service_nature",
"attributes": {
"id": 1,
"description": "Software Development",
"area_id": 1
},
"relationships": {
"area": {
"data": {
"id": "1",
"type": "area"
}
}
}
}
],
"included": [
{
"id": "1",
"type": "area",
"attributes": {
"id": 1,
"code": "01",
"description": "Headquarters"
}
}
],
"meta": {
"success": true,
"count": 1
}
}
我的模特:
Ext.define('Suite.model.MaintServiceNature', {
extend: 'Ext.data.Model',
fields: [
{ desc: "Id", name: 'id', type: 'int', useNull: true },
{ desc: "Area", name: 'area_id', type: 'int', useNull: true },
{ desc: "Description", name: 'description', type: 'string', useNull: true, tableIdentification: true }
],
associations: [
{
type: 'belongsTo',
model: 'Suite.model.Area',
foreignKey: 'area_id',
associationKey: 'area',
instanceName: 'Area',
getterName: 'getArea',
setterName: 'setArea',
reader: {
type: 'json',
root: false
}
}
],
proxy: {
type: 'rest',
url: App.getConf('restBaseUrlV2') + '/maint_service_natures',
reader: {
type: 'json',
root: 'data',
record: 'attributes',
totalProperty: 'meta.count',
successProperty: 'meta.success',
messageProperty: 'meta.errors'
}
}
});
关于如何设置关联以处理 v2 数据的任何想法?
【问题讨论】:
-
我认为您将不得不推出自己的阅读器类...该框架未设置为处理这些类型的结构。
标签: json extjs model associations extjs4.2