Skip to content

Instantly share code, notes, and snippets.

@runspired
Forked from Herriau/adapters.post.js
Last active April 13, 2021 18:31
Show Gist options
  • Save runspired/1d37a9df5b421993a7780a90959245c2 to your computer and use it in GitHub Desktop.
Save runspired/1d37a9df5b421993a7780a90959245c2 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,
meta: {},
links: {},
};
}
}
import Ember from 'ember';
import { inject as service } from '@ember/service';
export default Ember.Controller.extend({
});
import { helper } from '@ember/component/helper';
export default helper(function v(params/*, hash*/) {
switch (params[0]) {
case undefined:
return '[[UNDEFINED]]';
case null:
return '[[NULL]]';
default:
return params[0];
}
});
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();
const snapshot1 = post1.serialize({ [returnSnapshot]: true });
const snapshot2 = post2.serialize({ [returnSnapshot]: true });
console.log(snapshot2);
console.log(snapshot1.belongsTo('author')); // logs `null`
console.log(snapshot2.belongsTo('author')); // logs `undefined`, expected `null`.
return {
posts: [post1, post2],
authors: [
snapshot1.belongsTo('author'),
snapshot2.belongsTo('author')
]
};
}
});
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);
},
});
<h3>posts</h3>
<ul>
{{#each this.model.posts as |post|}}
<li>{{post.id}} {{post.title}}</li>
{{/each}}
</ul>
<h3>authors</h3>
<ul>
{{#each this.model.authors as |author|}}
<li>{{v author}}</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