Ember.js 余烬数据:我的自定义“serializeIntoHash”正在返回空哈希?
默认情况下,当RESTAdapter向服务器发送POST数据请求时,它会将模型的Ember.js 余烬数据:我的自定义“serializeIntoHash”正在返回空哈希?,ember.js,ember-data,Ember.js,Ember Data,默认情况下,当RESTAdapter向服务器发送POST数据请求时,它会将模型的typeKey作为散列的根发送: typeKey: { data } 但我的服务器需要一个“无根”哈希: { data } 我发现这是覆盖的方法,但我正在做的事情不仅导致根被删除,而且哈希本身也是空的。。。即使my console.log显示记录正在序列化到哈希中 import ApplicationSerializer from './application'; export default Applicat
typeKey
作为散列的根发送:
typeKey: { data }
但我的服务器需要一个“无根”哈希:
{ data }
我发现这是覆盖的方法,但我正在做的事情不仅导致根被删除,而且哈希本身也是空的。。。即使my console.log显示记录
正在序列化到哈希中
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
serializeIntoHash: function(hash, type, record, options) {
console.log(' hash going in: ' + JSON.stringify(hash)); // hash is {} going in
hash = this.serialize(record, options);
console.log('hash going out: ' + JSON.stringify(hash)); // hash is { full of data } going out
return hash; // after this, for some reason the request goes out as an empty hash {}
}
});
我是否没有正确返回修改后的哈希?我也尝试过这些变化:
return (hash, type, record, options)
及
及
我返回的任何东西似乎都不起作用。我不知道我做错了什么
我注意到,在中没有使用
返回
,但是如果我排除了我得到的是完全相同的问题,因此我不知道我是否需要返回?序列化为散列方法很时髦,调用方不希望您返回散列(如上所述),它希望您修改发送的散列
这意味着,如果您只是设置了散列,您将不再使用将被发送的散列。您需要设置属性/从该实例中删除属性
在这里,它们合并结果以完成我所说的:我的猜测是在您的
序列化到哈希中
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
serializeIntoHash: function(hash, type, record, options) {
console.log(' hash going in: ' + JSON.stringify(hash));
hash = this.serialize(record, options); // <---- THIS LINE
console.log('hash going out: ' + JSON.stringify(hash));
return hash;
}
});
你可以改变散列变量本身,而不是重新分配它的值
如果您还有任何问题,请告诉我。好的,这是有道理的。因此,如果我理解您的意思,我需要分别获取序列化(记录、选项)
的结果,并以某种方式循环并将结果中的属性添加到哈希?或。。。实际上,将散列与serialize()的结果合并,就像在您共享的链接中一样。。。哈哈,听起来容易些
return this._super(hash, type, record, options);
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
serializeIntoHash: function(hash, type, record, options) {
console.log(' hash going in: ' + JSON.stringify(hash));
hash = this.serialize(record, options); // <---- THIS LINE
console.log('hash going out: ' + JSON.stringify(hash));
return hash;
}
});
serializeIntoHash: function(hash, type, record, options) {
this._super(...arguments); // ES6 Syntax
// extract variable outside
Object.keys(hash.someInnerVariable).forEach(key => {
hash[key] = hash.someInnerVariable[key];
});
delete hash.someInnerVariable1
delete hash.someInnerVariable2
}