author | Chloe Laisne <chloe.laisne@gmail.com> |
Sat, 16 Jul 2016 17:23:47 +0200 | |
changeset 240 | aa101458cd4e |
parent 237 | 69a9f3687902 |
child 306 | 3fccf43160a7 |
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 |
} |
240
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
25 |
var array = []; |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
26 |
if(typeof request.queryParams.index !== 'undefined' && typeof request.queryParams.limit !== 'undefined') { |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
27 |
var begin = parseInt(request.queryParams.index * request.queryParams.limit) || 0; |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
28 |
var end = parseInt(begin + (request.queryParams.limit - 1)); |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
29 |
array = response.models.slice(begin, end); |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
30 |
} else { |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
31 |
array = response.models; |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
32 |
} |
aa101458cd4e
Player forward and backward
Chloe Laisne <chloe.laisne@gmail.com>
parents:
237
diff
changeset
|
33 |
return _(array).map((theme) => { return [theme.id, {count: theme.count, label: theme.label}];}).object().value(); |
173 | 34 |
} |
236
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
35 |
|
173 | 36 |
}); |