我正在使用Ember 2和Ember-data 2,并试图访问原始的json有效负载--标准的RESTAdapter从我的REST中获取并保存在存储中。在文档或其他地方找不到与此有关的任何信息。是创建自定义RESTAdapter的唯一选项吗?
发布于 2015-10-21 17:14:55
我这样做的方法是向模型中添加一个单独的属性,然后重写序列化程序。
首先你的模型:
export default DS.Model.extend({
rawJSON: DS.attr()
// Your other attributes...
});您的序列化程序(我正在使用JSONSerializer作为示例,但其他序列化程序应该非常类似):
export default DS.JSONSerializer.extend({
normalize(typeClass) {
// Simulate the extra attribute by adding it to the hash
hash.rawJSON = JSON.parse(JSON.stringify(json));
// Then let the serializer do the rest
return this._super.apply(this, arguments);
},
serialize(snapshot, options) {
// Let the serializer create the JSON
const json = this._super.apply(this, arguments);
// Remove the extra attribute we added
delete json.rawJSON;
return json;
}
});您可以选择通过覆盖应用程序序列化程序来对所有模型执行此操作,或者仅通过重写该类型的序列化程序来实现特定的模型。
https://stackoverflow.com/questions/33262879
复制相似问题