Skip to content

Instantly share code, notes, and snippets.

@rob-long
Forked from Alonski/controllers.application.js
Last active July 28, 2019 06:25
Show Gist options
  • Save rob-long/78992b6ed6b3b9a969de891d27b79df0 to your computer and use it in GitHub Desktop.
Save rob-long/78992b6ed6b3b9a969de891d27b79df0 to your computer and use it in GitHub Desktop.
ASYNC relationships ROBLONG
import Controller from '@ember/controller';
import { reads } from '@ember/object/computed';
import objectRegularPromise from 'twiddle/object-regular-promise';
import objectPromiseProxy from 'twiddle/object-promise-proxy';
export default Controller.extend({
children: reads('model'),
actions: {
async clickChild(child) {
const parentAsRegularPromise = objectRegularPromise(child.parent);
const parentAsPromiseProxy = objectPromiseProxy(child.parent);
console.log((await parentAsPromiseProxy).getCat());
}
}
});
export default function() {
this.resource('parent');
this.resource('child');
}
import { Factory } from 'ember-cli-mirage';
export default Factory.extend({
name(i) { return `Child[${i}]`; }
});
import { Factory } from 'ember-cli-mirage';
export default Factory.extend({
name(i) { return `Parent[${i}]`; }
});
export default function(server) {
let parent = server.create('parent');
server.createList('child', 10, { parent });
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
alwaysIncludeLinkageData: true,
});
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')
});
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'),
getCat() {
return 'cat';
}
});
import ObjectProxy from '@ember/object/proxy';
import RSVP from 'rsvp';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
const ObjectPromiseProxy = ObjectProxy.extend(PromiseProxyMixin);
export default function objectPromiseProxy(model) {
if (model instanceof ObjectProxy) {
const proxy = ObjectPromiseProxy.create({
promise: RSVP.resolve(model)
});
return proxy;
}
return model;
}
import ObjectProxy from '@ember/object/proxy';
import RSVP from 'rsvp';
export default function objectRegularPromise(model) {
return new RSVP.Promise(async (resolve, reject) => {
if (model instanceof ObjectProxy) {
try {
let awaitedModel = await model;
resolve(awaitedModel);
} catch (error) {
reject(error);
}
}
resolve(model);
});
}
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.store.findAll('child');
}
});
<ul>
{{#each children as |child|}}
<li {{action "clickChild" child}}>
{{child.name}}
:
{{#if child.parent.isPending}}
{{child.parent.name}}
Loading&hellip;
{{else}}
{{child.parent.name}}
{{/if}}
</li>
{{else}}
<li>No Bars!</li>
{{/each}}
</ul>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"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.7.0",
"ember-cli-mirage": "0.4.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment