cms/app-client/mirage/serializers/sparse-document.js
author Chloe Laisne <chloe.laisne@gmail.com>
Sun, 02 Oct 2016 15:27:15 +0200
changeset 312 cd62bbf96322
parent 275 a4d8618c2f1b
child 324 92fc9d077f95
permissions -rw-r--r--
Add hover state to discourse bubbles

// mirage/serializers/blog-post.js
import BaseSerializer from './application';
import _ from 'lodash';

export default BaseSerializer.extend({
    attrs: ['id', 'title', 'language', 'url', 'issued', 'modified', 'publishers', 'mediaArray', 'transcript'],

    serialize(response, request) {

        console.log(request, response);
        // This is how to call super, as Mirage borrows [Backbone's implementation of extend](http://backbonejs.org/#Model-extend)
        let json = BaseSerializer.prototype.serialize.apply(this, arguments);

        json['documents'] = _.map(json['documents'], function(doc) {
            let res = _.omit(doc, ['publishers', 'mediaArray', 'transcript']);
            res['publisher'] = doc['publishers'].join(', ');
            res['duration_ms'] = doc['mediaArray']?doc['mediaArray'][_(Object.keys(doc['mediaArray'])).first()]['extent_ms']:0;
            res['transcript_url'] = (doc['transcript'] && doc['transcript']['url'])?doc['transcript']['url']:null;
            return res;
        });

        return json;
    }

});