Skip to content

Instantly share code, notes, and snippets.

@arabyniuk
Created September 5, 2017 08:23
Show Gist options
  • Save arabyniuk/473c1e8c837bb04d48852c63e6cad1c5 to your computer and use it in GitHub Desktop.
Save arabyniuk/473c1e8c837bb04d48852c63e6cad1c5 to your computer and use it in GitHub Desktop.
import Ember from 'ember';
import DS from 'ember-data';
const { set, get } = Ember;
export default DS.JSONAPISerializer.extend({
serialize: function(snapshot, options) {
let json = this._super(snapshot, options);
const targetImagingAttributes = get(json, 'data.attributes.target-imaging-attributes');
if(targetImagingAttributes){
set(targetImagingAttributes, 'target-image-attributes', targetImagingAttributes.targetImage);
delete targetImagingAttributes.targetImage;
}
return json;
},
serializeBelongsTo: function(snapshot, json, relationship) {
let key = relationship.key;
let belongsTo = snapshot.belongsTo(key);
key = this.keyForRelationship ? this.keyForRelationship(key, 'belongsTo', 'serialize') : key;
key += '-attributes';
json.attributes[key] = Ember.isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON();
},
serializeHasMany: function(snapshot, json, relationship) {
let key = relationship.key;
let hasMany = snapshot.hasMany(key);
key = this.keyForRelationship ? this.keyForRelationship(key, 'hasMany', 'serialize') : key;
key += '-attributes';
let hasManyHash = {};
hasMany.map((snapshot, index) => {
hasManyHash[index] = snapshot.record.toJSON();
});
json.attributes[key] = hasManyHash;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment