我是一个项目的一部分,其中ExtJ必须用于前端开发。
后端开发人员将提供api,我必须使用它。
有几种API以以下格式返回json
{
"Entity": [
{
"id": 1,
"name": "Sunshine",
"placements": 3,
"next_meeting": "2019-03-14T16:29:00.48Z"
},
{
"id": 2,
"name": "Merlon",
"placements": 1,
"next_meeting": "2019-03-14T16:29:00.48Z"
},
{
"id": 3,
"name": "Mars",
"placements": 10,
"next_meeting": "2019-03-14T16:29:00.48Z"
},
{
"id": 4,
"name": "Moonlight",
"placements": 5,
"next_meeting": "2019-03-14T16:29:00.48Z"
}
],
"RecordCount": 4,
"IsSuccess": true,
"Unauthorized": false,
"ErrorInformation": {
"Message": null,
"Link": null
}
}
到目前为止,我正在创建如下所示的单独模型
ClientModel.js
Ext.define('ClientModel', {
extend: 'Ext.data.Model',
fields: [{
name: "id",
type: "int",
convert: null
},
{
name: "name",
type: "string"
},
{
name: "placements",
type: "int",
convert: null
},
{
name: "next_meeting",
type: "date",
dateFormat: 'm/d/Y',
convert: function(v, rec) {
return Ext.util.Format.date(v, 'm/d/Y');
}
}
],
proxy: {
type: "ajax",
url: "/api/Clients",
reader: {
type: "json",
rootProperty: "Entity",
totalProperty: "RecordCount",
successProperty: "IsSuccess"
}
}
});
寻找更好的方法,因为所有api均以给定的形式返回响应。
如何创建可以用作其他模型父级的基础模型?
我想设计模型如下:
Ext.define('ParentModel', {
extend: 'Ext.data.Model',
fields: [
{
name: 'Entity'
},
{
name: 'RecordCount',
type: 'int'
},
{
name: 'IsSuccess',
type: 'boolean'
},
{
name: 'Unauthorized',
type: 'boolean'
},
{
name: 'ErrorInformation'
}
]
});
现在让我说说api'/ api / clients'我得到一些描述了客户端模型的响应,因此Entity字段应该引用或指向ClientModel。
类似地,假设我有另一个描述注释模型的api'/ api / comments',现在实体字段应该引用或指向CommentModel。
答案 0 :(得分:0)
您可以创建ParentProxy:
Ext.define('ParentProxy', {
alias: 'proxy.parentProxy',
extend: 'Ext.data.proxy.Ajax',
type: "ajax",
url: "/api/Clients"
reader: {
type: "json",
rootProperty: "Entity",
totalProperty: "RecordCount",
successProperty: "IsSuccess"
}
});
接下来,您可以创建ParentModel:
Ext.define('ParentModel', {
extend: 'Ext.data.Model',
fields: [
{
name: "id",
type: "int",
convert: null
},
{
name: "name",
type: "string"
},
{
name: "placements",
type: "int",
convert: null
},
{
name: "next_meeting",
type: "date",
dateFormat: 'm/d/Y',
convert: function(v, rec) {
return Ext.util.Format.date(v, 'm/d/Y');
}
}
],
proxy: {
type: "parentProxy"
}
});
并创建ChildModel(如果字段相同):
Ext.define('ChildModel', {
extend: 'ParentModel',
proxy: {
url: "/api/ChildClients"
}
});
并创建ChildModel(如果字段不相同):
Ext.define('ChildModel2', {
extend: 'ParentModel',
fields: [
{
name: "id",
type: "int",
convert: null
},
{
name: "name",
type: "string"
},
{
name: "status",
type: "int"
}
],
proxy: {
url: "/api/ChildClients"
}
});
编辑: 我认为这是extjs中的不良模型模式。 您可以做的一件事就是模型中的关联。
例如reference,
fields: [{
name: 'Entity',
reference: 'CommentsModel'
}]
例如hasOne,它看起来像:
hasOne: [{
model: "CommentModel",
name:"Entity",
associationKey: "Entity"
}]