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-- |
173 | 1 |
import { JSONAPISerializer } from 'ember-cli-mirage'; |
2 |
||
3 |
import _ from 'lodash/lodash'; |
|
4 |
||
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 | 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 | 31 |
}); |