Skip to content

Instantly share code, notes, and snippets.

@averydev
Last active November 22, 2017 17:16
Show Gist options
  • Save averydev/c21429687f4414d69d93b46d7406ea4e to your computer and use it in GitHub Desktop.
Save averydev/c21429687f4414d69d93b46d7406ea4e to your computer and use it in GitHub Desktop.
Ember Concurrency Task Model / Nested Routes
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Concurrency Decendants'
});
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('blog',{path:"/blog/:blog_id"}, function (){
this.route('comments', {path:"/comments"});
});
});
export default Router;
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
export default Ember.Route.extend({
model: function (params){
return {
commentsTask: this.get('commentsTask').perform(),
comments:null
};
},
commentsTask: task(function *(){
let blog = yield this.modelFor('blog').blogTask;
yield timeout(1000);
let comments = [{title:"I love this blog!"}, {title:"I hate this blog!"}];
console.log("comments:", comments);
this.controller.set('model.comments', comments);
})
});
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
export default Ember.Route.extend({
model: function (){
return {
blog:null,
blogTask: this.get('blogTask').perform()};
},
blogTask: task(function *(){
yield timeout(1000);
let blog = {title: "This is a great Blog Post!"};
console.log(blog);
this.controller.set('model.blog', blog);
})
});
<h1>Ember Concurrency With Nested Models</h1>
<br>
{{#link-to "blog" 1}} BLOG {{/link-to}}<br>
{{#link-to "blog.comments" 1}} COMMENTS {{/link-to}}
<br>
{{outlet}}
<br>
<br>
<br><br>
Comments:
{{#if model.commentsTask.isRunning}}
LOADING COMMENTS...
{{else}}
{{#each model.comments as |aComment|}}
{{aComment.title}}<br>
{{/each}}
{{/if}}
{{#if model.blogTask.isRunning}}
LOADING BLOG POST...
{{else}}
Blog Title:{{model.blog.title}}<br>
{{/if}}
{{outlet}}
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1",
"ember-concurrency": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment