[英]Ember Data. How to sent to the server attributes which are not defined in model?
如果我从服务器收到额外的属性(未在模型中定义),我不会将它们存储在 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.