Estella Gonzalez Madison / @chicagoing
Dev Bootcamp NYC, 13 May 2014
Lots more
App.Book = Ember.Object.extend({
id: null,
title: null,
published: null,
authors: [], // An array of App.Author objects
description: null
});
App.Author = Ember.Object.extend({
id: null,
firstName: null,
lastName: null
});
App.Book = DS.Model.extend({
// Ember.Data automatically includes the id property
title: DS.attr('string'),
published: DS.attr('date'),
authors: DS.hasMany('author'), // Explicit relationship
description: DS.attr('string')
});
App.Author = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
<script type="text/x-handlebars">
<h1>List of books</h1>
<ol>
{{#each item in sortedBooks}}
<li>
<h2>{{item.title}}</h2>
<h3>{{item.author}}</h3>
<time>{{date item.published}}</time>
</li>
{{/each}}
</ol>
{{outlet}}
</script>
Every template requires a view, but Ember will automatically create one for you if you haven't
Create your own view when you need:
tagName
or elementId
classNameBindings
didInsertElement
, willDestroyElement
, etcApp.ApplicationView = Ember.View.extend({
elementId: "app",
tagName: "section",
didInsertElement: function() {
// You can initialize jQuery components here
},
click: function(event) {
...
}
});
<script type="text/x-handlebars">
{{share-twitter data-url=url data-text=text data-size="large" data-hashtags="lti"}}
</script>
App.ShareTwitterComponent = Ember.Component.extend({
tagName: 'a',
classNames: 'twitter-share-button',
attributeBindings: ['data-size', 'data-url', 'data-text', 'data-hashtags'],
didInsertElement: function() {
// Call Twitter API setup code
}
});
App.IndexController = Ember.ArrayController.extend({
// Content is usually set from the router & fetched w/ Ember.Data
content: null,
// You can add ':desc' to the property key to sort it
// in descending order.
propertiesToSortBy: ['published:desc', 'title', 'author'],
// The second argument is a property defining the sort criteria.
sortedBooks: Ember.computed.sort('content', 'propertiesToSortBy')
});
The URL is an important strength that the web has over native apps. In web apps, the URL goes beyond just being a static reference to the location of a document on a server. The best way to think of it is as the serialization of the application’s current state. As the user interacts with the application, the URL should update to reflect what the user is seeing on their screen.
App.Router.map(function() {
this.resource('books', { path: '/books' }, function() {
this.route('new');
});
// You can have dynamic segments (book_id)
this.resource('book', { path: '/book/:book_id' }, function() {
this.route('edit');
});
this.resource('author', { path: '/author/:author_id' }, function() {
this.route('edit');
});
});
Ember relies on naming conventions to wire up your objects without a lot of boilerplate (~Rails).
IndexRoute
IndexController
IndexView
index
template
These are automatically created for you until you create your own and add custom code & functionality.
SmashingMagazine, An In-Depth Introduction to Ember.js
Used to test user interaction and application flow