introToEmber



introToEmber

0 0


introToEmber

A reveal.js presentation on Ember.js

On Github davidatkinsondoyle / introToEmber

Intro to Ember.js

A framework for ambitious web applications

Me

  • Software and Data Architect
  • Freelance Developer

Overview

  • is an MVC framwork
  • has routing based architecture
  • attempts to remove boilerplate code and "take care" of the low level operations
  • has sudo Class/Instance objects
  • objects have getter and setters

Object Model

Classes and Instances

Class

App.Person = Ember.Object.extend({
  say: function(thing) {
    var name = this.get('name');
    alert(name + " says: " + thing);
  }
});

Instance

var person = App.Person.create();
person.say("Hello"); // alerts " says: Hello"

Computed Properties

App.Person = Ember.Object.extend({
  // these will be supplied by `create`
  firstName: null,
  lastName: null,

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName')
});
var ironMan = App.Person.create({
  firstName: "Tony",
  lastName:  "Stark"
});

ironMan.get('fullName'); // "Tony Stark"

Observers

Person = Ember.Object.extend({
  // these will be supplied by `create`
  firstName: null,
  lastName: null,

  fullName: function() {
    var firstName = this.get('firstName');
    var lastName = this.get('lastName');

    return firstName + ' ' + lastName;
  }.property('firstName', 'lastName'),

  fullNameChanged: function() {
    // deal with the change
  }.observes('fullName').on('init')
});
var person = Person.create({
  firstName: 'Yehuda',
  lastName: 'Katz'
});

person.set('firstName', 'Brohuda'); // observer will fire

Bindings

Run Loop

Templating

Handlebars


  
    <script type="text/x-handlebars">
      Hello, <strong>{{firstName}} {{lastName}}</strong>!
    </script>
  

						

Application Template

	<script type="text/x-handlebars">
	  <div>
	    {{outlet}}
	  </div>
	</script>
						

Routeing

Left Column

Right Column

The Router

App.Router.map(function() {
  this.route("about", { path: "/about" });
  this.route("favorites", { path: "/favs" });
});
- Router Defines the whole architecture of the app

Resourse and Dynamic Secments

App.Router.map(function() {
  this.resource('posts', { path: '/posts' }, function() {
    this.resource('post', { path: '/post/:post_id' });
  });
});
- defined my the model - updates url with id:property

The Route Object

    App.PersonsRoute = Ember.Route.extend({
        model: function() {
            return App.persons.getPersons();
        },
        setupController: function(controller, model) {
            controller.set('model', model);
        },
        beforeModel: function(transition) {
            if (!App.Persons) {
              return Ember.$.getScript('/models/persons.js');
            }
          }
        afterModel: function(posts, transition) {
            if (persons.get('length') === 1) {
              this.transitionTo('persons.show', persons.get('firstObject'));
            }
          }
    });
- model can be promise or sync - setupController is defult model=model

Controllers

Ember Contollers

  • There are three types of controllers
  • Defined by Model, Object vs Array
  • Act as a cache to the Model Data
  • Hold display logic
State not saved decorate models

Ember.Controller

Most simple controller, when there is no model.

Methods

create extend get set getProperties setProperties toString toggleProperty transitionToRoute init

...

Properties

actions model needs controllers queryParams

...

Ember.ObjectController

Used when model is an object

Has no additional methods Controller checks its own properties before it checks the model

Ember.ArrayController

Used when model is an Array (enumerable)

Methods

any compact contains every filter filterBy find findBy forEach indexOf
insertAt invoke map reduce popObject setEach setObjects shiftObject slice sortBy uniq

Properties

@each [] firstObject lastObject length model needs queryParams target length queryParams

Views

Ember Helpers

Custom Views

Setup and Teardown

- didInsertElement - willDestroyElement

Models