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 2: JSONAPIAdapter with JSONSerializer #1

Open
wants to merge 2 commits into
base: master
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

jsonapiadapter with jsonserializer

  • Loading branch information...
skaterdav85 committed Jul 19, 2019
commit 388c59a868e16ea70348ce6cc874d5da150376fb
@@ -0,0 +1,7 @@
import DS from 'ember-data';

const { JSONAPIAdapter } = DS;

export default JSONAPIAdapter.extend({

});
@@ -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 })
});
@@ -0,0 +1,7 @@
import DS from 'ember-data';

const { Model, attr } = DS;

export default Model.extend({
name: attr('string')
});
@@ -0,0 +1,7 @@
import Route from '@ember/routing/route';

export default Route.extend({
model() {
return this.store.findAll('post');
}
});
@@ -0,0 +1,21 @@
import DS from 'ember-data';

const { JSONSerializer, EmbeddedRecordsMixin } = DS;

export default JSONSerializer.extend(EmbeddedRecordsMixin, {
attrs: {
tags: {
embedded: 'always'
}
},
normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
let newPayload = payload.data.map(({ id, attributes }) => {
return {
id,
...attributes
};
});

return this._super(store, primaryModelClass, newPayload, id, requestType);
}
});
@@ -0,0 +1,5 @@
import DS from 'ember-data';

const { JSONSerializer } = DS;

export default JSONSerializer.extend();
@@ -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>
@@ -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"
}
]
}
}
]
});
});
}
@@ -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);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.