我正在实现JSON API结构(带有下划线属性)。
开发环境的实际状态是:
我使用Active Model Adapter结构向后端请求资源,后端使用JSON API结构响应我。
在我正在使用的应用程序序列化程序中JSONAPISerializer
。我覆盖方法:
serializeBelongsTo
keyForRelationship
keyForAttribute
serialize
serializeAttribute
serializeHasMany
对于开发,一切都对我有用(Rails 中的后端与 Ember 沟通得非常好)。
问题在于 Ember CLI Mirage 和约定(不确定是否有简单的解决方案,或者我需要再次覆盖此插件中的方法)。
Ember Cli Mirage 和测试环境的实际状态:
我正在使用import { JSONAPISerializer } from 'ember-cli-mirage';
然后尝试操作正确的请求,然后将其转换为 JSON API 格式。
它可以像这样工作:
Ember 适配器(活动模型适配器格式 - 带有下划线属性)---> Mirage Serializer 应该获取请求(查找之前在具有关联的测试中创建的资源),然后使用 JSON API 格式对其进行响应 ---> JSON API Serializer 可以捕获它并填充 Ember DS。
现在,我有一个缺失的部分来将所有情况下的它序列化为 JSON API 标准(带有下划线的属性)
我应该在哪里进行此转换以最小化覆盖 JSONAPISerializer Mirage Serializer。
我注意到有一些帮助者,但我有一个问题将这些知识包装在一起(http://www.ember-cli-mirage.com/docs/advanced/route-handlers#helpers)
更新:
来自后端的结构示例:
{
"data": {
"id": "6",
"type": "first_resource",
"attributes": {
"id": 6,
"my_attribute": "my_attribute"
},
"relationships": {
"second_resources": {
"data": [
{
"id": "16",
"type": "second_resource"
}
]
},
"third_resource_other_type": {
"data": {
"id": "1",
"type": "third_resource"
}
},
"fourth_resource": {
"data": {
"id": "1",
"type": "fourth_resource"
}
}
},
"links": {
"fifth_resources": "/api/v1/first_resources/6/fifth_resources"
}
},
"included": [
{
"id": "1",
"type": "fourth_resource",
"attributes": {
"id": 1,
"my_attribute": "my_attribute"
},
"links": {
"sixth_resource": "/api/v1/fourth_resources/1/sixth_resource"
}
},
{
"id": "16",
"type": "second_resource",
"attributes": {
"id": 16,
"my_attribute": "my_attribute"
},
"relationships": {
"eighth_resources": {
"data": []
}
},
"links": {
"seventh_resources": "/api/v1/second_resources/16/seventh_resources"
}
},
{
"id": "17",
"type": "second_resource",
"attributes": {
"id": 17,
"my_attribute": "my_attribute"
},
"relationships": {
"eighth_resources": {
"data": []
}
},
"links": {
"seventh_resources": "/api/v1/second_resources/17/seventh_resources"
}
},
{
"id": "15",
"type": "second_resource",
"attributes": {
"id": 15,
"my_attribute": "my_attribute"
},
"relationships": {
"eighth_resources": {
"data": [
{
"id": "26",
"type": "eighth_resource"
},
{
"id": "24",
"type": "eighth_resource"
}
]
}
},
"links": {
"seventh_resources": "/api/v1/second_resources/15/seventh_resources"
}
},
{
"id": "26",
"type": "eighth_resource",
"attributes": {
"id": 26,
"my_attribute": "my_attribute"
}
}
]
}
更新2
海市蜃楼响应的结构:
data: {
attributes: {
my_attribute: 'my_attribute',
second_resource_ids: [36, 37],
fifth_resource_ids: []
},
id: 11,
relationships: {
third_resource_other_type: {data: null}
fourth_resource: {data: null}
second_resources: {data: []}
},
type: "first_resources"
}
测试资源:
server.create('second-resource', {
id: 36,
first_resource_id: '11',
my_attribute: "my_attribute"
});
server.create('eighth-resource', {
id: 140,
second_resource_id: 37
});
server.create('eighth-resource', {
id: 141,
second_resource_id: 37
});
server.create('second-resource', {
id: 37,
first_resource_id: '11',
eighth_resource_ids: [140, 141]
});
server.create('first-resource', {
id: 11,
second_resource_ids: [36, 37]
});
mirage 中的 first_resource 模型:
export default Model.extend({
third_resource_other_type: belongsTo(),
fourth_resource: belongsTo(),
fifth_resources: hasMany(),
second_resources: hasMany()
});