Ghost/core/client/serializers/post.js
David Arvelo 8cbc6dc3b7 Calls to POST API have include=tags
closes #2998
- update PostSerializer to use DS.EmbeddedRecordsMixin
- create PostAdapter to include include=tags in query params for POST and PUT
- set include=tags for various GET post requests
- change PostModel to have { embedded: always } instead of { async: true }
- update Ember-Data to beta8 from beta7
- make call to get tags from model in editor.edit route synchronous since the tags now exist in the store
- change casper test to wait for call to posts api with `?include=tags`
2014-06-28 03:13:31 -04:00

54 lines
1.8 KiB
JavaScript

import ApplicationSerializer from 'ghost/serializers/application';
var PostSerializer = ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
// settings for the EmbeddedRecordsMixin.
attrs: {
tags: { embedded: 'always' }
},
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';
}
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];
}
});
export default PostSerializer;