Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo 1: JSONAPIAdapter with JSONAPISerializer #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/adapters/application.js
@@ -0,0 +1,7 @@
import DS from 'ember-data';

const { JSONAPIAdapter } = DS;

export default JSONAPIAdapter.extend({

});
8 changes: 8 additions & 0 deletions app/models/post.js
@@ -0,0 +1,8 @@
import DS from 'ember-data';

const { Model, attr, hasMany } = DS;

export default Model.extend({
title: attr('string'),
tags: hasMany('tag', { async: false })
});
7 changes: 7 additions & 0 deletions app/models/tag.js
@@ -0,0 +1,7 @@
import DS from 'ember-data';

const { Model, attr } = DS;

export default Model.extend({
name: attr('string')
});
7 changes: 7 additions & 0 deletions app/routes/application.js
@@ -0,0 +1,7 @@
import Route from '@ember/routing/route';

export default Route.extend({
model() {
return this.store.findAll('post');
}
});
38 changes: 38 additions & 0 deletions app/serializers/post.js
@@ -0,0 +1,38 @@
import DS from 'ember-data';

const { JSONAPISerializer } = DS;

export default JSONAPISerializer.extend({
normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
payload.data.forEach((resource) => {
resource.relationships = {
tags: {
data: resource.attributes.tags.map((tag) => {
return {
id: tag.id,
type: 'tags'
};
})
}
};
});

payload.included = payload.data
.map((resource) => {
return resource.attributes.tags;
})
.flat()
.map((tag) => {
let resource = {
id: tag.id,
type: 'tags'
};

delete tag.id;
resource.attributes = tag;
return resource;
});

return this._super(...arguments);
}
});
19 changes: 14 additions & 5 deletions app/templates/application.hbs
@@ -1,5 +1,14 @@
{{!-- The following component displays Ember's default welcome message. --}}
<WelcomePage />
{{!-- Feel free to remove this! --}}

{{outlet}}
<ul>
{{#each model as |post|}}
<li>
{{post.title}}
<ul>
{{#each post.tags as |tag|}}
<li>
{{tag.name}}
</li>
{{/each}}
</ul>
</li>
{{/each}}
</ul>
46 changes: 46 additions & 0 deletions mirage/config.js
@@ -0,0 +1,46 @@
import { Response } from 'ember-cli-mirage';

export default function() {
this.get('/posts', () => {
return new Response(200, {}, {
data: [
{
id: '1',
type: 'post',
attributes: {
title: 'Post A',
body: '...',
tags: [
{
id: '1',
'name': 'JavaScript'
},
{
id: '2',
name: 'Node.js'
}
]
}
},
{
id: '2',
type: 'post',
attributes: {
title: 'Post B',
body: '...',
tags: [
{
id: '1',
'name': 'JavaScript'
},
{
id: '3',
name: 'Ember.js'
}
]
}
}
]
});
});
}
9 changes: 9 additions & 0 deletions mirage/scenarios/default.js
@@ -0,0 +1,9 @@
export default function(/* server */) {

/*
Seed your development database using your factories.
This data will not be loaded in your tests.
*/

// server.createList('post', 10);
}