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-08-19 12:55:40 +01:00
|
|
|
export default ApplicationSerializer.extend(DS.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-09-03 12:06:50 +01:00
|
|
|
normalize: function (typeClass, hash, prop) {
|
2014-07-30 22:44:51 -04:00
|
|
|
// this is to enable us to still access the raw author_id
|
|
|
|
// without requiring an extra get request (since it is an
|
|
|
|
// async relationship).
|
|
|
|
hash.author_id = hash.author;
|
|
|
|
|
2015-09-03 12:06:50 +01:00
|
|
|
return this._super(typeClass, hash, prop);
|
2014-07-30 22:44:51 -04:00
|
|
|
},
|
|
|
|
|
2015-09-03 12:06:50 +01:00
|
|
|
normalizeSingleResponse: function (store, primaryModelClass, payload) {
|
|
|
|
var root = this.keyForAttribute(primaryModelClass.modelName),
|
|
|
|
pluralizedRoot = Ember.String.pluralize(primaryModelClass.modelName);
|
2014-06-26 22:35:25 -04:00
|
|
|
|
|
|
|
payload[root] = payload[pluralizedRoot][0];
|
|
|
|
delete payload[pluralizedRoot];
|
|
|
|
|
|
|
|
return this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2015-09-03 12:06:50 +01:00
|
|
|
normalizeArrayResponse: function () {
|
|
|
|
return this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2014-06-26 22:35:25 -04:00
|
|
|
serializeIntoHash: function (hash, type, record, options) {
|
|
|
|
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-06-02 20:56:42 -06:00
|
|
|
var root = Ember.String.pluralize(type.modelName),
|
2014-06-26 22:35:25 -04:00
|
|
|
data = this.serialize(record, options);
|
|
|
|
|
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.
|
|
|
|
delete data.author_id;
|
|
|
|
// 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
|
|
|
}
|
|
|
|
});
|