Ghost/core/client/app/serializers/post.js
Kevin Ansfield 3f361f8742 Standardize on var-less export default across ember app
no issue
- drops the `var Foo = Ember.Thing.extend({}); export default Foo;` syntax in favour of exporting directly, eg: `export default Ember.Thing.extend({})`
- discussion on this change [here](https://github.com/TryGhost/Ghost/pull/5340#issuecomment-105828423) and [here](https://github.com/TryGhost/Ghost/pull/5694#discussion-diff-37511606)
2015-10-06 10:59:50 +01:00

55 lines
1.9 KiB
JavaScript

import Ember from 'ember';
import DS from 'ember-data';
import ApplicationSerializer from 'ghost/serializers/application';
export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
// settings for the EmbeddedRecordsMixin.
attrs: {
tags: {embedded: 'always'}
},
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);
},
extractSingle: function (store, primaryType, payload) {
var root = this.keyForAttribute(primaryType.modelName),
pluralizedRoot = Ember.String.pluralize(primaryType.modelName);
// 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);
},
serializeIntoHash: function (hash, type, record, options) {
options = options || {};
options.includeId = true;
// We have a plural root in the API
var root = Ember.String.pluralize(type.modelName),
data = this.serialize(record, options);
// Properties that exist on the model but we don't want sent in the payload
delete data.uuid;
delete data.html;
// Inserted locally as a convenience.
delete data.author_id;
// Read-only virtual property.
delete data.url;
hash[root] = [data];
}
});