Skip to content

Instantly share code, notes, and snippets.

@Herriau
Created July 6, 2020 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Herriau/e54c989135437db7edba65d2fa440b1e to your computer and use it in GitHub Desktop.
Save Herriau/e54c989135437db7edba65d2fa440b1e to your computer and use it in GitHub Desktop.
New Twiddle
import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default class PostAdapter extends JSONAPIAdapter {
async findRecord(store, type, id) {
switch (id) {
case '1':
return {
"data": {
"id": "1",
"type": "posts",
"links": {
"self": "http:\/\/localhost:3000\/posts\/1"
},
"attributes": {
"title": "Foo"
},
"relationships": {
"author": {
"links": {
"self": "http:\/\/localhost:3000\/posts\/1\/relationships\/author",
"related": "http:\/\/localhost:3000\/posts\/1\/author"
},
"data": null
}
}
}
};
case '2':
return {
"data": {
"id": "2",
"type": "posts",
"links": {
"self": "http:\/\/localhost:3000\/posts\/2"
},
"attributes": {
"title": "Bar"
},
"relationships": {
"author": {
"links": {
"self": "http:\/\/localhost:3000\/posts\/2\/relationships\/author",
"related": "http:\/\/localhost:3000\/posts\/2\/author"
}
}
}
}
};
}
}
async findBelongsTo(store, snapshot, url) {
return {
"data": null
};
}
}
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({
title: attr('string'),
author: belongsTo('author', { inverse: 'posts' }),
});
import Ember from 'ember';
import { returnSnapshot } from '../serializers/application';
import { all } from 'rsvp';
export default Ember.Route.extend({
async model() {
const post1 = await this.store.findRecord('post', 1, { include: 'author' });
const post2 = await this.store.findRecord('post', 2);
await post2.belongsTo('author').load();
return [post1, post2];
},
afterModel([post1, post2]) {
const snapshot1 = post1.serialize({ [returnSnapshot]: true });
const snapshot2 = post2.serialize({ [returnSnapshot]: true });
console.log(snapshot1.belongsTo('author')); // logs `null`
console.log(snapshot2.belongsTo('author')); // logs `undefined`, expected `null`.
}
});
import JSONAPISerializer from 'ember-data/serializers/json-api';
export const returnSnapshot = Symbol();
export default JSONAPISerializer.extend({
serialize(snapshot, options) {
if (options[returnSnapshot]) {
return snapshot;
}
return this._super(...arguments);
},
});
<h2>I am {{model.name}}, here are my posts:</h2>
<ul>
{{#each model as |post|}}
<li>{{post.id}} {{post.title}}</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