author | Chloe Laisne <chloe.laisne@gmail.com> |
Sat, 09 Jul 2016 00:59:32 +0200 | |
changeset 236 | ac6928e86d14 |
parent 229 | 744379451219 |
child 275 | a4d8618c2f1b |
permissions | -rw-r--r-- |
173 | 1 |
// mirage/serializers/blog-post.js |
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 | 4 |
|
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 | 24 |
}); |