Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created July 4, 2017 16:41
Show Gist options
  • Save lifeart/8ecb068314c4f180b71f8aa528a0aa04 to your computer and use it in GitHub Desktop.
Save lifeart/8ecb068314c4f180b71f8aa528a0aa04 to your computer and use it in GitHub Desktop.
Application model serializer
import DS from 'ember-data';
import ClubSerializer from '../serializers/application';
export default ClubSerializer.extend({
extractAttributes(modelClass, resourceHash) {
var attributes = {};
resourceHash['origin'] = resourceHash.video_id;
resourceHash['file-name'] = resourceHash.file;
resourceHash['thumbnail-offset'] = resourceHash.img;
resourceHash['duration'] = resourceHash.len_val;
resourceHash['duration-formatted'] = resourceHash.len;
resourceHash['thumbnail'] = resourceHash.thumb;
resourceHash['thumbnail-uri'] = resourceHash.thumb_url;
resourceHash['created-at'] = resourceHash.date;
resourceHash['description'] = resourceHash.desc;
delete resourceHash['video_id'];
delete resourceHash['file'];
delete resourceHash['img'];
delete resourceHash['len_val'];
delete resourceHash['len'];
delete resourceHash['thumb'];
delete resourceHash['thumb_url'];
delete resourceHash['date'];
delete resourceHash['desc'];
if (resourceHash) {
modelClass.eachAttribute((key) => {
let attributeKey = this.keyForAttribute(key, 'deserialize');
if (resourceHash[attributeKey] !== undefined) {
attributes[key] = resourceHash[attributeKey];
}
});
}
return attributes;
},
extractRelationships(modelClass, resourceHash) {
let relationships = {};
resourceHash.relationships = {
category: {
data: {
id: resourceHash.category || 0,
type: 'video-category'
}
},
channel: {
data: {
id: resourceHash.channel || 0,
type: 'partner-channel'
}
}
};
if (resourceHash.relationships) {
modelClass.eachRelationship((key, relationshipMeta) => {
let relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
if (resourceHash.relationships[relationshipKey] !== undefined) {
let relationshipHash = resourceHash.relationships[relationshipKey];
relationships[key] = this.extractRelationship(relationshipHash);
}
});
}
return relationships;
},
_normalizeResourceHelper(resourceHash) {
resourceHash.type = this._extractType();
return this._super(resourceHash);
},
_extractType(modelClass, resourceHash) {
return 'partner-video';
},
extractId(modelClass,resourceHash) {
return resourceHash['uid'];
},
serialize(snapshot, options) {
let jsonAPIResult = this._super(...arguments).data;
let result = {
thumb: jsonAPIResult.attributes['thumbnail-offset'],
name: jsonAPIResult.attributes['name'],
desc: jsonAPIResult.attributes['description'],
channel: jsonAPIResult.relationships.channel.data.id,
category: jsonAPIResult.relationships.category.data.id,
uid: snapshot.id,
};
return result;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment