Skip to content

Instantly share code, notes, and snippets.

@Gaurav0
Forked from spicalous/controllers.application\.js
Last active December 14, 2020 19:02
Show Gist options
  • Save Gaurav0/af0a823fc73474db1c09083c429fef4f to your computer and use it in GitHub Desktop.
Save Gaurav0/af0a823fc73474db1c09083c429fef4f to your computer and use it in GitHub Desktop.
Loading Children
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
export default function() {
this.get('/parents');
this.get('/parents/:id', { timing: 2000 });
}
export default function(server) {
server.create('parent', {
name: 'parent 1',
children: [
server.create('child', { name: 'child 1' }),
server.create('child', { name: 'child 2' }),
server.create('child', { name: 'child 3' })
]
});
}
import Model, { attr, hasMany } from '@ember-data/model';
export default class extends Model {
@attr('string') name;
}
import Model, { attr, hasMany } from '@ember-data/model';
export default class extends Model {
@attr('string') name;
@hasMany('child') children;
get childrenLoading() {
return this.hasMany('children').load().isPending;
}
}
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('second-route', { path: '/second-route/:parent_id' });
});
export default Router;
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.store.findAll('parent');
}
});
import Route from '@ember/routing/route';
export default Route.extend({
model(params) {
return this.store.findRecord('parent', params.parent_id, {
include: 'children'
});
}
});
<h1>App route</h1>
{{#each this.model as |parent|}}
<div>{{parent.name}}</div>
<LinkTo @route="second-route" @model={{parent.id}}>
Go to second route with this model
</LinkTo>
{{/each}}
{{outlet}}
<h1>Second route</h1>
<div>model.isLoading: {{this.model.isLoading}}</div>
<div>model.isLoaded: {{this.model.isLoaded}}</div>
<div>model.isReloading: {{this.model.isReloading}}</div>
<br>
<br>
<div>model.children.isPending: {{this.model.children.isPending}}</div>
<div>model.children.isSettled: {{this.model.children.isSettled}}</div>
<br>
<br>
<div>model.childrenLoading: {{this.model.childrenLoading}}</div>
<br>
<br>
<div>
Parent name: {{this.model.name}}
</div>
{{#each this.model.children as |child|}}
<div>{{child.name}}</div>
{{else}}
{{#if this.model.childrenLoading}}
<div>Loading...</div>
{{else}}
<div>Nothing to display</div>
{{/if}}
{{/each}}
{
"version": "0.17.1",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-data": "3.18.0",
"ember-cli-mirage": "1.1.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment