Skip to content

Instantly share code, notes, and snippets.

@DanGe42
Last active July 13, 2019 01:29
Show Gist options
  • Save DanGe42/e43dd130c7a0ef3d2bdaa663a7339996 to your computer and use it in GitHub Desktop.
Save DanGe42/e43dd130c7a0ef3d2bdaa663a7339996 to your computer and use it in GitHub Desktop.
belongsTo test
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember belongsTo test'
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
parent: belongsTo('parent'),
name: attr('string')
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
parent: belongsTo('parent'),
name: attr('string')
});
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'),
childA: belongsTo('child-a'),
childB: belongsTo('child-b')
});
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('test', { path: '/' });
this.route('test');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
let parent = this.store.peekRecord('parent', 1);
if (!parent) {
this._setup();
parent = this.store.peekRecord('parent', 1);
console.log(parent.childA.get('name'));
return parent;
} else {
return parent;
}
},
_setup() {
this.store.push({
data: [
{
id: 1,
type: 'child-a',
attributes: {
name: 'Child A'
}
},
{
id: 1,
type: 'child-b',
attributes: {
name: 'Child B'
}
},
]
});
this.store.push({
data: [
{
id: 1,
type: 'parent',
attributes: {
name: 'Parent'
},
relationships: {
childA: {
data: {
id: 1,
type: 'child-a'
}
},
childB: {
data: {
id: 1,
type: 'child-b'
}
}
}
}
]
});
}
});
<h1>Welcome to {{appName}}</h1>
<p>outlet</p>
<br>
{{outlet}}
<br>
<p>/outlet</p>
<p>Hello</p>
<div>
{{model.childA.name}}
</div>
{
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment