project/app/serializers/useful.js

39 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2016-04-06 15:53:06 +00:00
import DS from 'ember-data';
2016-04-07 15:45:00 +00:00
import Ember from 'ember';
2016-04-06 15:53:06 +00:00
export default DS.JSONAPISerializer.extend({
2016-04-07 15:45:00 +00:00
serializeBelongsTo: function(record, json, relationship) {
var key = relationship.key;
var belongsTo = Ember.get(record, key);
key = this.keyForRelationship ? this.keyForRelationship(key, 'belongsTo') : key;
2016-04-06 15:53:06 +00:00
2016-04-07 15:45:00 +00:00
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);
2016-04-06 15:53:06 +00:00
2016-04-07 15:45:00 +00:00
if (relationship.options.embedded === 'always') {
if (hasMany && relationshipType === 'manyToNone' || relationshipType === 'manyToMany' ||
relationshipType === 'manyToOne') {
2016-04-06 15:53:06 +00:00
2016-04-07 15:45:00 +00:00
json[key] = [];
hasMany.forEach(function(item) {
json[key].push(item.serialize());
});
}
2016-04-06 15:53:06 +00:00
2016-04-07 15:45:00 +00:00
} else {
return this._super(record, json, relationship);
2016-04-06 15:53:06 +00:00
}
2016-04-07 15:45:00 +00:00
}
2016-04-06 15:53:06 +00:00
});