Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active March 20, 2019 22:41
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 runspired/fb1cd61a85d6b206d4d688c6733f400e to your computer and use it in GitHub Desktop.
Save runspired/fb1cd61a85d6b206d4d688c6733f400e to your computer and use it in GitHub Desktop.
Relationship Reload Issue
import { Promise } from 'rsvp';
export default class AppAdapter {
constructor(options) {
Object.assign(this, options);
}
static create(options) {
return new this(options);
}
findRecord() {
return Promise.resolve({
data: {
type: 'person',
id: '1',
attributes: { name: 'Chris' },
relationships: {
father: {
data: null,
links: {
related: './person/1/father/'
}
}
}
}
});
}
findBelongsTo() {
return new Promise(resolve => {
setTimeout(resolve, 3000);
}).then(() => { return {
data: {
type: 'person',
id: '2',
attributes: { name: 'John' },
/*
relationships: {
child: {
data: {
type: 'person', id: '1'
}
}
}
*/
}
}});
}
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
findUser() {
let store = this.get('store');
return store.findRecord('person', '1')
.then(person => {
this.set('user', person);
let father = person.belongsTo('father').reload();
return father;
});
},
init() {
this._super(...arguments);
this.findUser();
}
});
import Model from 'ember-data/model';
import Ember from 'ember';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr(),
father: belongsTo('person', {
async: false,
inverse: 'child'
}),
child: belongsTo('person', {
async: false,
inverse: 'father'
}),
});
export default class MySerializer {
constructor(options) {
Object.assign(this, options);
}
static create(options) {
return new this(options);
}
normalizeResponse(_, __, data) { return data; }
}
<h1>Welcome to {{appName}}, {{user.name}}</h1>
My Id {{user.id}}.<br>My state is: <b>{{user.currentState.stateName}}</b>
<br>
{{user.father.name}} is your father. His ID is {{user.father.id}}.<br>His state is: <b>{{user.father.currentState.stateName}}</b>
<br>
{{outlet}}
<br>
<br>
{
"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.8.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment