cms/app-client/mirage/serializers/theme.js
author Chloe Laisne <chloe.laisne@gmail.com>
Tue, 12 Jul 2016 09:48:11 +0200
changeset 237 69a9f3687902
parent 236 ac6928e86d14
child 240 aa101458cd4e
permissions -rw-r--r--
Scrolling system in thematiques: more button and append data on scroll
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
import { JSONAPISerializer } from 'ember-cli-mirage';
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import _ from 'lodash/lodash';
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 JSONAPISerializer.extend({
236
ac6928e86d14 Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents: 173
diff changeset
     6
ac6928e86d14 Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents: 173
diff changeset
     7
    serialize: function(response, request) {
237
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
     8
        // Remove models with no name
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
     9
        response.models = response.models.filter(element => element.label);
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    10
        // Alphabetical order
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    11
        if(request.queryParams.sort === 'alphabetical') {
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    12
            response.models.sort(function(a, b) {
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    13
                var aLabel = a.label.toUpperCase();
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    14
                var bLabel = b.label.toUpperCase();
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    15
                if (aLabel < bLabel) return -1;
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    16
                if (aLabel > bLabel) return 1;
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    17
                return 0;
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    18
            });
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    19
        // Descending order
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    20
        } else if(request.queryParams.sort === 'descending') {
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    21
            response.models.sort(function(a, b) {
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    22
                return b.count - a.count;
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    23
            });
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    24
        }
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    25
        var begin = parseInt(request.queryParams.index * request.queryParams.limit);
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    26
        var end = parseInt(begin + (request.queryParams.limit - 1));
69a9f3687902 Scrolling system in thematiques: more button and append data on scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 236
diff changeset
    27
        var slice = response.models.slice(begin, end);
236
ac6928e86d14 Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents: 173
diff changeset
    28
        return _(slice).map((theme) => { return [theme.id, {count: theme.count, label: theme.label}];}).object().value();
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    }
236
ac6928e86d14 Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents: 173
diff changeset
    30
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
});