cheatsheets/ember.md

74 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2014-02-25 10:32:14 +00:00
---
title: Ember.js
2017-08-28 19:56:17 +00:00
category: JavaScript libraries
tags: [Archived]
archived: This sheet describes an older version of Ember.
2014-02-25 10:32:14 +00:00
---
2017-08-28 15:51:51 +00:00
{% raw %}
2014-02-25 10:32:14 +00:00
### Routes
Router.map(function() {
this.route('trips', function() {
this.route('item', { path: '/:tripId' });
2014-02-25 10:32:14 +00:00
});
this.route('upcoming');
this.route('about', { path: '/aboutus' });
2014-02-25 10:32:14 +00:00
this.route('schedules');
this.route('history');
this.route('post', { path: '/post/:postId' });
2014-02-25 10:32:14 +00:00
});
### A route
import Route from '@ember/routing/route';
export default PostRoute extends Route {
model({ postId }) {
// Post will be accessible as `this.model` in the controller
// or `{{@model}}` in the template.
return this.store.find('post', postId);
2014-02-25 10:32:14 +00:00
}
}
### Component
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
export default PostEditor extends Component {
@tracked title;
get fullTitle() {
return `Post: ${title}`;
2014-02-25 10:32:14 +00:00
}
titleUpdate(event) {
this.title = event.target.value;
}
}
2014-02-25 10:32:14 +00:00
### Template
2014-03-22 04:04:21 +00:00
<div ...attributes>
<label for="title">Title</label>
<input
id="title"
value={{@post.title}}
{{on 'input' this.updateTitle}}
/>
2014-03-22 04:04:21 +00:00
<p>
{{this.fullTitle}}
</p>
</div>
2014-03-22 04:04:21 +00:00
Invoking the component:
2014-03-22 04:04:21 +00:00
<PostEditor class='my-post' @post={{@model}} />
2014-02-25 10:32:14 +00:00
2017-08-28 15:51:51 +00:00
{% endraw %}