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: {
|
|
|
|
tags: { embedded: 'always' }
|
|
|
|
},
|
|
|
|
|
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 || {};
|
|
|
|
|
|
|
|
// We have a plural root in the API
|
|
|
|
var root = Ember.String.pluralize(type.typeKey),
|
|
|
|
data = this.serialize(record, options);
|
|
|
|
|
|
|
|
// Don't ever pass uuid's
|
|
|
|
delete data.uuid;
|
|
|
|
|
|
|
|
hash[root] = [data];
|
2014-06-13 04:48:15 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default PostSerializer;
|