【发布时间】:2015-07-23 10:01:17
【问题描述】:
我正在使用 Ember 和 Ember-data。但是我收到的 JSON 不符合 Ember 侧载标准。 JSON 没有根模型。模型也是嵌入的,有时有 Id,有时没有 Id。
我看到了几个关于如何使用 extract 钩子添加根模型以及如何使用嵌入模型的链接
App.ColorSerializer = DS.RestSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
foos: {embedded: 'always'}
}
});
此代码取自this链接。
这是这里使用的 JSON
{
colors:[
{
id: 1,
color: "red",
foos:[
{
id:1,
name:'something 1'
},
{
id:2,
name:'something 2'
}
]
},
...
现在我面临的问题是我的 JSON 也可能如下所示(没有根模型“颜色”)
{
id: 1,
color: "red",
foos:[
{
id:1,
name:'something 1'
},
{
id:2,
name:'something 2'
}
]
},
...
甚至像这样(没有 foo 对象的 ID)
{
id: 1,
color: "red",
foos:[
{
name:'something 1'
},
{
name:'something 2'
}
]
},
...
有什么办法可以解决这个问题吗?如何将 ID 添加到嵌入式模型 foo?还有一些解决方案/插件可以接受任何类型的嵌入式 JSON 并将其转换为侧面加载的 JSON 并在需要时添加 Id。
我已经看到this 解决方案。它真的有效吗?因为它没有使用最新的EmbeddedRecordsMixin
【问题讨论】:
标签: ember.js ember-data