Skip to content

Instantly share code, notes, and snippets.

@timrwood
Created April 2, 2018 17:26
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 timrwood/e3279bc44fab6a3e63673fe06b2bfa0d to your computer and use it in GitHub Desktop.
Save timrwood/e3279bc44fab6a3e63673fe06b2bfa0d to your computer and use it in GitHub Desktop.
Ember Data meta relationship
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
parents: null,
actions: {
clear() {
this.store.unloadAll('parent');
this.store.unloadAll('child');
this.set('model', null);
},
loadParent() {
this.store.pushPayload({
data: {
id: 1,
type: 'parent',
relationships: {
children: {
data: [
{ id: 1, type: 'child' },
{ id: 2, type: 'child' }
]
}
}
},
included: [
{ id: 1, type: 'child' },
{ id: 2, type: 'child' }
]
});
console.log('loadParent')
this.set('model', this.store.peekAll('parent'));
},
loadMeta() {
this.store.pushPayload({
data: {
id: 1,
type: 'parent',
relationships: {
children: {
meta: { included: false }
}
}
}
});
console.log('loadMeta')
this.set('model', this.store.peekAll('parent'));
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
children: hasMany('child')
});
import DS from 'ember-data'
export default DS.JSONAPISerializer.extend({
extractRelationships(modelClass, resourceHash) {
let relationships = this._super(modelClass, resourceHash)
console.log(relationships)
return relationships
},
})
{{#each model as |parent|}}
Parent {{parent.id}} has
{{#each parent.children as |child|}}
child {{child.id}}
{{/each}}
{{/each}}
<br/>
<button {{action "loadParent"}}>Load Parent with children</button>
<button {{action "loadMeta"}}>Load Parent without children</button>
<button {{action "clear"}}>Clear</button>
{
"version": "0.13.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment