Automatically update count.posts when serialising tags in mirage

no issue
- add a new mirage serialiser for tags that updates the count.posts for each tag that gets serialised
- better matches real API behaviour
This commit is contained in:
Kevin Ansfield 2018-11-08 13:49:00 +00:00
parent ae0cb45d8c
commit 9abadd57c4
2 changed files with 20 additions and 3 deletions

View File

@ -14,8 +14,7 @@ export default Factory.extend({
updatedAt: '2015-10-19T16:25:07.756Z', updatedAt: '2015-10-19T16:25:07.756Z',
updatedBy: 1, updatedBy: 1,
count() { count() {
return { // this gets updated automatically by the tag serializer
posts: 1 return {posts: 0};
};
} }
}); });

View File

@ -0,0 +1,18 @@
import BaseSerializer from './application';
export default BaseSerializer.extend({
// make the tag.count.posts value dynamic
serialize(tagModelOrCollection, request) {
let updatePostCount = (tag) => {
tag.update('count', {posts: tag.postIds.length});
};
if (this.isModel(tagModelOrCollection)) {
updatePostCount(tagModelOrCollection);
} else {
tagModelOrCollection.models.forEach(updatePostCount);
}
return BaseSerializer.prototype.serialize.call(this, tagModelOrCollection, request);
}
});