project/app/serializers/useful.js
2016-04-07 16:45:00 +01:00

39 lines
1.1 KiB
JavaScript

import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONAPISerializer.extend({
serializeBelongsTo: function(record, json, relationship) {
var key = relationship.key;
var belongsTo = Ember.get(record, key);
key = this.keyForRelationship ? this.keyForRelationship(key, 'belongsTo') : key;
if (relationship.options.embedded === 'always') {
json[key] = belongsTo.serialize();
} else {
return this._super(record, json, relationship);
}
},
serializeHasMany: function(record, json, relationship) {
var key = relationship.key;
var hasMany = Ember.get(record, key);
var relationshipType = DS.RelationshipChange.determineRelationshipType(
record.constructor,
relationship);
if (relationship.options.embedded === 'always') {
if (hasMany && relationshipType === 'manyToNone' || relationshipType === 'manyToMany' ||
relationshipType === 'manyToOne') {
json[key] = [];
hasMany.forEach(function(item) {
json[key].push(item.serialize());
});
}
} else {
return this._super(record, json, relationship);
}
}
});