如果我从服务器收到额外的属性(模型中未定义),则不会将其存储在ember模型中。因此,在PUT
或POST
上,我不会将它们发送到服务器。
是否有一种方法可以将这些额外的属性保留为“散列数据”,然后将它们发送回服务器?
您可以通过汇总用于发送和接收数据的串行器来做到这一点,例如,如果您使用的是JSONAPIAdapter,则使用如下的序列化器:
export default class YouRouteSerializer extends JSONAPISerializer {
//for sending data
serialize(snapshot, options) {
let json = super.serialize(...arguments);
//add a vlue that doesnt exist in the model
json.data.attributes.theNewAttributeName = 'example';
// delete a value
delete json.data.attributes.theAttributeNameToBeDeleted;
return json;
}
//and for receiving (this is what you are looking for)
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
let extratData = payload.data.attributes.yourExtratData;
// here store you extrat data somewhere else , localstorage for example
// thenn
// delete your extra data
delete payload.data.attributes.yourExtratData;
return super.normalizeResponse(...arguments);
}
}
这里是自定义序列化程序Customizing Serializers的参考