2014-06-13 04:48:15 +04:00
|
|
|
import ApplicationSerializer from 'ghost/serializers/application';
|
|
|
|
|
2014-06-27 06:35:25 +04:00
|
|
|
var PostSerializer = ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
|
|
|
|
// settings for the EmbeddedRecordsMixin.
|
|
|
|
attrs: {
|
2014-10-25 01:09:50 +04:00
|
|
|
tags: {embedded: 'always'}
|
2014-06-27 06:35:25 +04:00
|
|
|
},
|
|
|
|
|
2014-07-31 06:44:51 +04:00
|
|
|
normalize: function (type, hash) {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
return this._super(type, hash);
|
|
|
|
},
|
|
|
|
|
2014-06-27 06:35:25 +04:00
|
|
|
extractSingle: function (store, primaryType, payload) {
|
|
|
|
var root = this.keyForAttribute(primaryType.typeKey),
|
|
|
|
pluralizedRoot = Ember.String.pluralize(primaryType.typeKey);
|
|
|
|
|
|
|
|
// make payload { post: { title: '', tags: [obj, obj], etc. } }.
|
|
|
|
// this allows ember-data to pull the embedded tags out again,
|
|
|
|
// in the function `updatePayloadWithEmbeddedHasMany` of the
|
|
|
|
// EmbeddedRecordsMixin (line: `if (!partial[attribute])`):
|
|
|
|
// https://github.com/emberjs/data/blob/master/packages/activemodel-adapter/lib/system/embedded_records_mixin.js#L499
|
|
|
|
payload[root] = payload[pluralizedRoot][0];
|
|
|
|
delete payload[pluralizedRoot];
|
|
|
|
|
|
|
|
return this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
keyForAttribute: function (attr) {
|
|
|
|
return attr;
|
|
|
|
},
|
|
|
|
|
|
|
|
keyForRelationship: function (relationshipName) {
|
|
|
|
// this is a hack to prevent Ember-Data from deleting our `tags` reference.
|
|
|
|
// ref: https://github.com/emberjs/data/issues/2051
|
|
|
|
// @TODO: remove this once the situation becomes clearer what to do.
|
|
|
|
if (relationshipName === 'tags') {
|
|
|
|
return 'tag';
|
2014-06-13 04:48:15 +04:00
|
|
|
}
|
2014-06-27 06:35:25 +04:00
|
|
|
|
|
|
|
return relationshipName;
|
|
|
|
},
|
|
|
|
|
|
|
|
serializeIntoHash: function (hash, type, record, options) {
|
|
|
|
options = options || {};
|
2014-12-18 21:39:11 +03:00
|
|
|
options.includeId = true;
|
2014-06-27 06:35:25 +04:00
|
|
|
|
|
|
|
// We have a plural root in the API
|
|
|
|
var root = Ember.String.pluralize(type.typeKey),
|
|
|
|
data = this.serialize(record, options);
|
|
|
|
|
2014-12-15 18:01:30 +03:00
|
|
|
// Properties that exist on the model but we don't want sent in the payload
|
|
|
|
|
2014-06-27 06:35:25 +04:00
|
|
|
delete data.uuid;
|
2014-09-04 00:51:52 +04:00
|
|
|
delete data.html;
|
2014-12-15 18:01:30 +03:00
|
|
|
// Inserted locally as a convenience.
|
|
|
|
delete data.author_id;
|
|
|
|
// Read-only virtual property.
|
|
|
|
delete data.url;
|
2014-06-27 06:35:25 +04:00
|
|
|
|
|
|
|
hash[root] = [data];
|
2014-06-13 04:48:15 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default PostSerializer;
|