Introduction
to Ember.js
May 2014: Tech Talk
What is Ember?
"A Framework for creating ambitious web applications"
Single Page App
Javascript App
Long Lived Javascript App
What does that mean?
- Single full page load
- Required libraries
- Code to wire-up the app
- Optionally, required DOM structure
- Subsequent XHRs
- Get model data
- Optionally, view templates and application assets
- Event handling
- Manipulate the DOM
- More XHRs
- Repeat
Ember is MVC*
*sort of
More like MVC+R
I don't want to see even one more blog post that uses the term "MVC" to describe (and attack) a pattern that many frameworks use.
— Yehuda Katz (@wycats) April 30, 2014
Ember Lifecycle
- Route
- Fetches model data
- Creates model instances
- Optionally handles user defined actions
- Controller
- Receives model
- Decorates model with additional properties
- Passes data backing the view
- Optionally handles user defined actions
Ember Lifecycle
- View
- Handles DOM events
- Renders Templates
- Templates
- Handlebars Templates
- Mustache Syntax
Router
The Router contains your route names and controls the URLs
App.Router.map(function() {
this.resource('students', function() {
this.route('top');
this.resource('student', { path: '/:student_id' });
});
});
Routes and the Router is one reason Ember is special
"you got some good routing there, ember.js...would be a shame if something HAPPENED TO URLS" -google
— JennpireJS (@jennschiffer) May 2, 2014
Routes
- Responsible for fetching the model
- Plain XHR
- jQuery $.ajax ($.getJSON )
- Ember-Model
- Ember-Data
App.StudentsRoute = Ember.Route.extend({
model: function() {
return Ember.RSVP.cast($.getJSON('/api/students'));
}
});
Controllers
- The bridge between the model and the view
- Decorates the model with additional properties
App.StudentsController = Ember.ArrayController.extend({
males: function() {
return this.get('content').filterBy('gender', 'male').get('length');
}.property('content.@each.gender'),
females: function() {
return this.get('content').filterBy('gender', 'female').get('length');
}.property('content.@each.gender')
});
Views
Typically you won't have to define them UNLESS
- You need specific handling of user events
- You want to create a re-usable component
Templates
- Use Handlebars syntax
- Can be defined 'in-line' via <script>
- Are precompiled for production use
<ul>
{{#each item in model itemController="student"}}
<li>{{full_name}}<li>
{{/each}}
</ul>
Males: {{males}}<br>
Females: {{females}}
References
Intro to Ember
By Ryan Hirsch
Intro to Ember
- 915