Skip to content

Instantly share code, notes, and snippets.

@sly7-7
Forked from Herriau/adapters.author.js
Created May 21, 2021 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sly7-7/2511332d43d8d9192ba2af3829bd2deb to your computer and use it in GitHub Desktop.
Save sly7-7/2511332d43d8d9192ba2af3829bd2deb to your computer and use it in GitHub Desktop.
EmberData: Unnecessary inverse relationship requests
import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default class AuthorAdapter extends JSONAPIAdapter {
async findRecord(store, type, id) {
console.log(`Requesting http://localhost:3000/authors/${id}`);
return {
"data": {
"id": "1",
"type": "authors",
"links": {
"self": "http:\/\/localhost:3000\/authors\/1"
},
"attributes": {
"name": "Zelda"
},
"relationships": {
"posts": {
"links": {
"self": "http:\/\/localhost:3000\/authors\/1\/relationships\/posts",
"related": "http:\/\/localhost:3000\/authors\/1\/posts"
},
"data": [
{
"type": "posts",
"id": "1"
},
{
"type": "posts",
"id": "2"
},
{
"type": "posts",
"id": "3"
},
{
"type": "posts",
"id": "4"
}
]
}
}
},
"included": [
{
"id": "1",
"type": "posts",
"links": {
"self": "http:\/\/localhost:3000\/posts\/1"
},
"relationships": {
"author": {
"links": {
"self": "http:\/\/localhost:3000\/posts\/1\/relationships\/author",
"related": "http:\/\/localhost:3000\/posts\/1\/author"
}
}
}
},
{
"id": "2",
"type": "posts",
"links": {
"self": "http:\/\/localhost:3000\/posts\/2"
},
"relationships": {
"author": {
"links": {
"self": "http:\/\/localhost:3000\/posts\/2\/relationships\/author",
"related": "http:\/\/localhost:3000\/posts\/2\/author"
}
}
}
},
{
"id": "3",
"type": "posts",
"links": {
"self": "http:\/\/localhost:3000\/posts\/3"
},
"relationships": {
"author": {
"links": {
"self": "http:\/\/localhost:3000\/posts\/3\/relationships\/author",
"related": "http:\/\/localhost:3000\/posts\/3\/author"
}
}
}
},
{
"id": "4",
"type": "posts",
"links": {
"self": "http:\/\/localhost:3000\/posts\/4"
},
"relationships": {
"author": {
"links": {
"self": "http:\/\/localhost:3000\/posts\/4\/relationships\/author",
"related": "http:\/\/localhost:3000\/posts\/4\/author"
}
}
}
}
]
};
}
async findBelongsTo(store, snapshot) {
throw new Error('Should not get here');
}
}
import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default class PostAdapter extends JSONAPIAdapter {
async findBelongsTo(store, snapshot, url) {
console.log(`Requesting ${url}`);
return {
"data": {
"id": "1",
"type": "authors",
"attributes": {
"name": "Zelda"
},
"relationships": {
"posts": {
"links": {
"self": "http:\/\/localhost:3000\/authors\/1\/relationships\/posts",
"related": "http:\/\/localhost:3000\/authors\/1\/posts"
}
}
}
}
};
}
}
import Ember from 'ember';
import { inject as service } from '@ember/service';
export default Ember.Controller.extend({
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr('string'),
posts: hasMany('post', { inverse: 'author' }),
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
author: belongsTo('author', { inverse: 'posts' }),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findRecord('author', 1, {
include: 'posts'
});
}
});
import JSONAPISerializer from 'ember-data/serializers/json-api';
export default JSONAPISerializer.extend({});
<h2>I am {{model.name}}, here are my posts:</h2>
<ul>
{{#each model.posts as |post|}}
<li>{{post.id}} (written by {{post.author.name}})</li>
{{/each}}
</ul>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.16.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment