Skip to content

Instantly share code, notes, and snippets.

@alexlafroscia
Last active September 2, 2017 13:48
Show Gist options
  • Save alexlafroscia/5523c451fa871770c8c84eaeb01a1fcb to your computer and use it in GitHub Desktop.
Save alexlafroscia/5523c451fa871770c8c84eaeb01a1fcb to your computer and use it in GitHub Desktop.
Presentation & Container Components in Ember
import Ember from 'ember';
const BlogCommentComponent = Ember.Component.extend({
});
BlogCommentComponent.reopenClass({
positionalParams: ['comment']
});
export default BlogCommentComponent;
<hr />
{{comment.body}}
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
const COMMENTS = [
{ body: 'This is a comment' },
{ body: 'This is another comment' }
];
export default Ember.Component.extend({
tagName: '',
init() {
this._super(...arguments);
this.set('comments', []);
},
didInsertElement() {
this._super(...arguments);
this.get('loadComments').perform();
},
loadComments: task(function* () {
// We use a timeout here to simulate the fact that fetching the comments takes some time
// If we did not lazily load them, the initial page load would be extended by this 1000ms instead
yield timeout(1000);
this.set('comments', COMMENTS);
})
});
import Ember from 'ember';
const { computed } = Ember;
const BlogPostComponent = Ember.Component.extend({
title: computed.alias('blogPost.title'),
body: computed.alias('blogPost.body')
});
BlogPostComponent.reopenClass({
positionalParams: ['blogPost']
});
export default BlogPostComponent;
<h1>{{title}}</h1>
{{body}}
{{#blog-post/comments-for blogPost as |comments|}}
{{#each comments as |comment|}}
{{blog-comment comment}}
{{/each}}
{{/blog-post/comments-for}}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
blogPost: {
title: 'This is the blog post',
body: 'We want to render the comments later on, after the main content has loaded. This makes a better experience for the user, since they get the content they care about immediately, and we can fill in less important stuff after the initial page load.'
}
});
{{blog-post blogPost}}
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": true,
"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": "0.8.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment