cms/app-client/mirage/serializers/sparse-document.js
author ymh <ymh.work@gmail.com>
Sat, 06 Aug 2016 21:27:53 +0700
changeset 260 64caee7ce38d
parent 229 744379451219
child 275 a4d8618c2f1b
permissions -rw-r--r--
Split document model in document and document results
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
// mirage/serializers/blog-post.js
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import BaseSerializer from './application';
229
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     3
import _ from 'lodash';
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
export default BaseSerializer.extend({
229
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     6
    attrs: ['id', 'title', 'language', 'url', 'issued', 'modified', 'publishers', 'mediaArray'],
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     7
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     8
    serialize(response, request) {
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     9
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    10
        console.log(request, response);
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    11
        // This is how to call super, as Mirage borrows [Backbone's implementation of extend](http://backbonejs.org/#Model-extend)
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    12
        let json = BaseSerializer.prototype.serialize.apply(this, arguments);
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    13
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    14
        json['documents'] = _.map(json['documents'], function(doc) {
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    15
            let res = _.omit(doc, ['publishers', 'mediaArray']);
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    16
            res['publisher'] = doc['publishers'].join(', ');
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    17
            res['duration_ms'] = doc['mediaArray']?doc['mediaArray'][_(Object.keys(doc['mediaArray'])).first()]['extent_ms']:0;
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    18
            return res;
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    19
        });
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    20
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    21
        return json;
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    22
    }
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    23
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
});