Skip to content

Instantly share code, notes, and snippets.

@abuiles
Created July 7, 2016 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abuiles/d2ee2599e9963a6c870dcf57a8ed0d5f to your computer and use it in GitHub Desktop.
Save abuiles/d2ee2599e9963a6c870dcf57a8ed0d5f to your computer and use it in GitHub Desktop.
// jscs:disable requireSpread
import V2Serializer from './v2';
import Ember from 'ember';
export default V2Serializer.extend({
_normalizeBossAndAssistant(resourceObject, object) {
let bosses = window.server.schema.employeeAssistants.where({
assistantId: object.id
}).models.mapBy('employee');
resourceObject.relationships.bosses = resourceObject.relationships.bosses || { data: [] };
resourceObject.relationships.bosses.data = bosses.map(function(e) {
return {
id: e.id,
type: 'employees'
};
});
let assistants = window.server.schema.employeeAssistants.where({
employeeId: object.id
}).models.mapBy('assistant');
resourceObject.relationships.assistants = resourceObject.relationships.assistants || { data: [] };
resourceObject.relationships.assistants.data = assistants.map(function(e) {
return {
id: e.id,
type: 'employees'
};
});
},
serialize(object) {
let json = V2Serializer.prototype.serialize.apply(this, arguments);
/**
Adolfo Builes - 7/7/2015
Mirage has some bugs with reflective has-many-to-many
relationships. Not 100% sure yet, but I think is
https://github.com/samselikoff/ember-cli-mirage/issues/755
As a workaround we are creating a "join" model, taking
out the employees and creating the JSON manually. C'est la vie.
*/
if (Ember.isArray(json.data)) {
json.data.forEach((resourceObject, index) => {
this._normalizeBossAndAssistant(resourceObject, object.models[index]);
});
} else {
this._normalizeBossAndAssistant(json.data, object);
}
return json;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment