这是一个经典的情况,在ember-data可以使用它之前需要更改收到的JSON,以及它如何看待这些转换:
{
"profile":{
"first_name":"fsdf",
"last_name":"sdfsdf",
"company":null,
"birthday":null,
"emails":[
{
"address":"sdfsdfsdf",
"id":1,
"kind":"main",
"contactable_type":"Profile",
"contactable_id":1
}
],
"addresses":[
],
"id":1
}
}
在模型的序列化程序提取钩子中完成:
export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
emails: { embedded: 'always' },
addresses: { embedded: 'always' }
},
extract: function(store, typeClass, payload, id, requestType) {
let profile = normalizeProfile(payload, id),
profileJSON = requestType == 'findAll' ? { profiles: [profile] } : { profile: profile };
return this._super(store, typeClass, profileJSON, id, requestType);
},
});
和this._super(store,typeClass,profileJSON,id,requestType);返回
{
"company":null,
"birthday":null,
"emails":[
1
],
"addresses":[
],
"id":1,
"firstName":"fsdf",
"lastName":"sdfsdf"
}
当它第一次保存时,一切正常,但是当个人资料再次保存时,电子邮件会重复 - 商店中有两个有id,一个没有id。地址和种类属性是相同的。 Ember-data 1.13.4
的样子