2015-10-28 11:36:45 +00:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2015-02-12 21:22:32 -07:00
|
|
|
import Ember from 'ember';
|
|
|
|
import DS from 'ember-data';
|
2014-06-12 20:48:15 -04:00
|
|
|
import ApplicationSerializer from 'ghost/serializers/application';
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
const {EmbeddedRecordsMixin} = DS;
|
|
|
|
|
|
|
|
export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
|
2014-06-26 22:35:25 -04:00
|
|
|
// settings for the EmbeddedRecordsMixin.
|
|
|
|
attrs: {
|
2014-10-24 21:09:50 +00:00
|
|
|
tags: {embedded: 'always'}
|
2014-06-26 22:35:25 -04:00
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
normalize(typeClass, hash, prop) {
|
2016-01-23 19:12:22 +01:00
|
|
|
// this is to enable us to still access the raw authorId
|
2014-07-30 22:44:51 -04:00
|
|
|
// without requiring an extra get request (since it is an
|
|
|
|
// async relationship).
|
2016-01-23 19:12:22 +01:00
|
|
|
hash.authorId = hash.author;
|
2014-07-30 22:44:51 -04:00
|
|
|
|
2015-09-03 12:06:50 +01:00
|
|
|
return this._super(typeClass, hash, prop);
|
2014-07-30 22:44:51 -04:00
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
normalizeSingleResponse(store, primaryModelClass, payload) {
|
|
|
|
let root = this.keyForAttribute(primaryModelClass.modelName);
|
|
|
|
let pluralizedRoot = Ember.String.pluralize(primaryModelClass.modelName);
|
2014-06-26 22:35:25 -04:00
|
|
|
|
|
|
|
payload[root] = payload[pluralizedRoot][0];
|
|
|
|
delete payload[pluralizedRoot];
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
return this._super(...arguments);
|
2014-06-26 22:35:25 -04:00
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
normalizeArrayResponse() {
|
|
|
|
return this._super(...arguments);
|
2015-09-03 12:06:50 +01:00
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
serializeIntoHash(hash, type, record, options) {
|
2014-06-26 22:35:25 -04:00
|
|
|
options = options || {};
|
2014-12-18 18:39:11 +00:00
|
|
|
options.includeId = true;
|
2014-06-26 22:35:25 -04:00
|
|
|
|
|
|
|
// We have a plural root in the API
|
2015-10-28 11:36:45 +00:00
|
|
|
let root = Ember.String.pluralize(type.modelName);
|
|
|
|
let data = this.serialize(record, options);
|
2014-06-26 22:35:25 -04:00
|
|
|
|
2014-12-15 15:01:30 +00:00
|
|
|
// Properties that exist on the model but we don't want sent in the payload
|
|
|
|
|
2014-06-26 22:35:25 -04:00
|
|
|
delete data.uuid;
|
2014-09-03 20:51:52 +00:00
|
|
|
delete data.html;
|
2014-12-15 15:01:30 +00:00
|
|
|
// Inserted locally as a convenience.
|
2016-01-23 19:12:22 +01:00
|
|
|
delete data.authorId;
|
2014-12-15 15:01:30 +00:00
|
|
|
// Read-only virtual property.
|
|
|
|
delete data.url;
|
2014-06-26 22:35:25 -04:00
|
|
|
|
|
|
|
hash[root] = [data];
|
2014-06-12 20:48:15 -04:00
|
|
|
}
|
|
|
|
});
|