Skip to content

Instantly share code, notes, and snippets.

@runspired
Forked from Alonski/controllers.application.js
Last active March 16, 2019 19:37
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/13004a0eae8fddb543e7705d372eb1b0 to your computer and use it in GitHub Desktop.
Save runspired/13004a0eae8fddb543e7705d372eb1b0 to your computer and use it in GitHub Desktop.
ember-data belongsTo proxy unresolved REPL
import EmberObject from '@ember/object';
import { resolve } from 'rsvp';
export default class ApplicationAdapter extends EmberObject {
findRecord() {
return resolve({
data: {
type: 'parent',
id: '1',
attributes: {
name: 'John'
},
relationships: {
children: {
data: [
{ type: 'child', id: '1' },
{ type: 'child', id: '2' }
]
}
}
},
included: [
{
type: 'child',
id: '1',
attributes: {
name: 'Chris'
},
},
{
type: 'child',
id: '2',
attributes: {
name: 'Jim'
},
}
]
});
}
}
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(),
parent: belongsTo('parent', {
async: false,
inverse: 'children'
})
});
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(),
children: hasMany('child', {
// it does not matter whether this is true or false
async: false,
inverse: 'parent'
})
});
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.get('store').findRecord('parent', '1');
},
actions: {
checkInJs(child) {
console.log({
child,
dotParent: child.parent,
getParent: child.get('parent')
});
}
}
});
export default class ApplicationSerializer {
constructor(config) {
Object.assign(this, config);
}
static create(config) {
return new this(config);
}
normalizeResponse(_, __, data) { return data; }
}
<ul>
{{#each model.children as |child|}}
<li>
<i>{{child.name}}</i>
<b>has parent</b>
<u>{{child.parent.name}}</u>
<br><br>
Status: <button {{action "checkInJs" child}}>Log Me</button><br>
{{#if child.parent._belongsToState}}
<ul>
<li>Has Content:
<b>{{if child.parent.content 'true' 'false'}}</b>
</li>
<li>Has Promise:
<b>{{if child.parent.promise 'true' 'false'}}</b>
</li>
<li>Fulfilled:
<b>{{if child.parent.isFulfilled 'true' 'false'}}</b>
</li>
<li>Pending:
<b>{{if child.parent.isPending 'true' 'false'}}</b>
</li>
<li>Settled:
<b>{{if child.parent.isSettled 'true' 'false'}}</b>
</li>
<li>Rejected:
<b>{{if child.parent.isRejected 'true' 'false'}}</b>
</li>
<li>Truthy:
<b>{{if child.parent.isTruthy 'true' 'false'}}</b>
</li>
</ul>
<br>
<i>This relationship is a proxy.</i><br><span style="font-size: .75em">Change the 'parent' relationship on the 'child' model to "async: false" to make it the record.</span>
{{else}}
<i>This relationship is not a proxy.</i><br><span style="font-size: .75em">Change the 'parent' relationship on the 'child' model to "async: true" to make it a proxy.</span>
{{/if}}
<hr>
</li>
{{else}}
<li>No Children!</li>
{{/each}}
</ul>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"ENV": {},
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment