我试图让嵌套关系为我的api工作但到目前为止没有成功。每个"报告"可以有多个"节点"。 这是我的模特:
//model report.js
export default DS.Model.extend({
// == Relationships =========================================================
nodes: hasMany('node', {async: false})// to check this inverse: null
})
//model node.js
export default DS.Model.extend({
changeType: attr('string'),
report: belongsTo('report')
})
这是我的report.js序列化程序
// serializer for report.js
export default DS.JSONAPISerializer.extend({
keyForAttribute: function (attr) {
return attr
},
keyForRelationship: function (key) {
return key
},
normalizeResponse: function (store, primaryModelClass, payload, id, requestType) {
return this._super(store, primaryModelClass, this.getNewPayload(payload), id, requestType)
},
getNewPayload (payload) {
let newPayload = {
data: []
}
if (!isNone(payload)) {
let nodeId = 1
if (payload.data instanceof Array) {
let nodeRelationship = []
let included = []
payload.data.map((record) => {
nodeRelationship.pushObject({id: nodeId, type: 'node'})
included.pushObject({id: nodeId, type: 'node', attributes: record.node}) // attributes: record.node
nodeId++
})
newPayload.data.push({
id: 0,
type: 'report',
attributes: {},
relationships: {
nodes: {
data: nodeRelationship
}
},
included: included
})
}
}
return newPayload
}
})
这是我的数据的样子。
export default {
"data": [
{
"node": {
"changeType": "added",
"name": "node1"
}
},
{
"node": {
"changeType": "removed",
"name": "nodel2"
}
}
]
}
当我检查ember-data时,我不确定我在做错了什么,节点模型是空的。我感谢任何帮助