cms/app-client/mirage/serializers/sparse-document.js
author ymh <ymh.work@gmail.com>
Tue, 11 Oct 2016 16:39:11 +0200
changeset 327 13564bb13ccc
parent 324 92fc9d077f95
child 348 a5e8d037304b
permissions -rw-r--r--
In the serialized document the lan files id 'languages' and not 'language'
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';
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
     4
import utils from './utils';
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
export default BaseSerializer.extend({
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 229
diff changeset
     7
    attrs: ['id', 'title', 'language', 'url', 'issued', 'modified', 'publishers', 'mediaArray', 'transcript'],
229
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     8
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
     9
    serialize(response, request) {
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    10
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
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    14
        let page = Math.max(parseInt(request.queryParams.page || 1) - 1, 0);
327
13564bb13ccc In the serialized document the lan files id 'languages' and not 'language'
ymh <ymh.work@gmail.com>
parents: 324
diff changeset
    15
        let perPage = parseInt(request.queryParams.perpage || 100);
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    16
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    17
        json['documents'] = _.map(_.slice(json['documents'], page*perPage, (page+1)*perPage), function(doc) {
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 229
diff changeset
    18
            let res = _.omit(doc, ['publishers', 'mediaArray', 'transcript']);
229
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    19
            res['publisher'] = doc['publishers'].join(', ');
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    20
            res['duration_ms'] = doc['mediaArray']?doc['mediaArray'][_(Object.keys(doc['mediaArray'])).first()]['extent_ms']:0;
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 229
diff changeset
    21
            res['transcript_url'] = (doc['transcript'] && doc['transcript']['url'])?doc['transcript']['url']:null;
229
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    22
            return res;
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    23
        });
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    24
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    25
        let meta = {
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    26
          total: response.models.length,
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    27
          per_page: perPage,
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    28
          current_page: page+1,
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    29
          last_page: Math.floor(response.models.length/perPage)+1,
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    30
          from: page*perPage + 1,
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    31
          to: Math.min((page+1)*perPage, response.models.length),
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    32
          prev_page_url: null,
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    33
          next_page_url: null
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    34
        };
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    35
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    36
        let urlParts = utils.parseUri(request.url);
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    37
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    38
        if(page>0) {
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    39
          let prevUrlParts = _.clone(urlParts);
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    40
          prevUrlParts.queryKey.page = page;
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    41
          meta.prev_page_url = utils.mergeUri(prevUrlParts);
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    42
        }
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    43
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    44
        if(page<(meta.last_page-1)) {
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    45
          let nextUrlParts = _.clone(urlParts);
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    46
          nextUrlParts.queryKey.page = page+2;
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    47
          meta.next_page_url = utils.mergeUri(nextUrlParts);
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    48
        }
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    49
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    50
        json['meta'] = meta;
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    51
229
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    52
        return json;
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    53
    }
744379451219 add publisher and duration_ms to model
ymh <ymh.work@gmail.com>
parents: 222
diff changeset
    54
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
});
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    56
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    57
// total	3373
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    58
// per_page	15
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    59
// current_page	1
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    60
// last_page	225
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    61
// next_page_url	"http://localhost:8000/api/v1/documents?page=2"
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    62
// prev_page_url
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    63
// from	1
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    64
// to	15