Ember-Data: Is my custom `serializeIntoHash` returning an empty hash?
By default, when the RESTAdapter sends a request to the server for POST data, it sends the model typeKey
as the hash root:
typeKey: { data }
but my server wants a "no root" hash:
{ data }
I found that this is a method to overwrite, but something I am doing is not only causing the root to be deleted, but the hash itself is empty too ... although my console.log shows it is record
serializing to a hash.
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 {}
}
});
Am I not returning the changed hash correctly? I've also tried these options:
return (hash, type, record, options)
and
return this._super(hash);
and
return this._super(hash, type, record, options);
I won't return anything, it seems to work. I don't know what I am doing wrong?
I noticed in the API Docs for the method , no return
, but if I rule out I get the same question, so I don't know if I even need to return or not?
source to share
The method is serializeIntoHash
funky, the caller doesn't expect you to return a hash (as noted), it expects you to change the hash that was sent.
This means that if you just set the hash, you will no longer have to deal with the hash being removed. You will need to set properties / remove properties from this instance.
Here they combine the results to accomplish what I say: https://github.com/emberjs/data/blob/v1.0.0-beta.10/packages/ember-data/lib/serializers/json_serializer.js#L500
source to share
My guess is in your serializeIntoHash
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;
}
});
It has something to do with javascript closure. hash
the parameter value does not change, instead javascript created a copy hash
and reassigned the new value. When the function is executed, the parameter hash
remains unchanged, the variable hash
is discarded, and the Ember rest.js source code does not expect a return value.
What can you do is
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
}
You get the idea, change the hash variable itself, rather than reassign its value.
Let me know if you have further questions.
source to share