Skip to content

Instantly share code, notes, and snippets.

@lolmaus
Last active September 5, 2019 15:08
Show Gist options
  • Save lolmaus/9832dfe40ba75f18be131a9fd49a0595 to your computer and use it in GitHub Desktop.
Save lolmaus/9832dfe40ba75f18be131a9fd49a0595 to your computer and use it in GitHub Desktop.
Two-way relationships defined as one-way on the backend side
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
child: Ember.computed(function () {
return this.store.peekRecord('child', '1');
}),
init () {
this.store.push({
data: {
id: '1',
type: 'parent',
attributes: {
name: 'Stan',
},
relationships: {
children: {
data: [
{id: '1', type: 'child'},
]
}
}
}
});
this.store.push({
data: {
id: '1',
type: 'child',
attributes: {
name: 'Steve',
},
}
});
}
});
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'),
parent: belongsTo('parent', {async: false}),
});
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'),
children: hasMany('child', {async: false}),
});
<h1>Welcome to {{appName}}</h1>
<br>
Child: {{this.child.name}}
<br>
Parent: {{this.child.parent.name}}
<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.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment