12

Click here to load reader

Introduction to Ember

Embed Size (px)

DESCRIPTION

Sam Goldman's intro to Ember, which helps developers build the same product, with less code.

Citation preview

Page 1: Introduction to Ember

Introduction to EmberJSSmartLogic ConfAugust 30, 2012

Page 2: Introduction to Ember

Ember Components

• Router

• Controllers

• Views

• Templates

• Bindings

Page 3: Introduction to Ember

Ember Router

• State machine for your application

• URLs are states

• Actions are transitions

Page 4: Introduction to Ember

Router ExampleTodo.Router = Ember.Router.extend location: "none" root: Ember.Route.extend index: Ember.Route.extend route: "/" connectOutlets: (router) -> items = Todo.Item.find() router.get("applicationController"). connectOutlet("list", items) view: (router, event) -> router.transitionTo("show", event.context) show: Ember.Route.extend route: "/:item" connectOutlets: (router, item) -> router.get("applicationController").connectOutlet("item", item) back: (router, event) -> router.transitionTo("index")

Page 5: Introduction to Ember

Controllers

• Three stock flavors:

• Controller

• ObjectController

• ArrayController

• Wrap model data for views

• Provide context-specific behaviors

Page 6: Introduction to Ember

Controller Example

Z.RecentActivityController = Ember.ArrayController.extend content: (-> billPayments = @get("billPayments") or [] payments = @get("payments") or [] billPayments.toArray().slice(0, 10). concat(payments.toArray().slice(0,10)) ).property("billPayments.@each", "payments.@each") sortProperties: ["createdAt"]

Page 7: Introduction to Ember

Views

• Handles events seamlessly

• No need to manage event listeners!

• Comes with a few baked-in patterns:

• CollectionView

• ContainerView

• Cool 3rd party libs, like ember-bootstrap

Page 8: Introduction to Ember

View ExampleEmber.Checkbox = Ember.View.extend({ classNames: ['ember-checkbox'], tagName: 'input', attributeBindings: [ 'type', 'checked', 'disabled', 'tabindex' ], type: "checkbox", checked: false, disabled: false});

Page 9: Introduction to Ember

Templates

• Handlebars with Ember additions

• #view

• #outlet

• #action

• Quick and familiar way to compose views

Page 10: Introduction to Ember

Template Example

<script type="text/x-handlebars" data-template-name="list"> <ul> {{#each controller}} <li> <label> {{view Ember.Checkbox checkedBinding="done"}} {{name}} </label> </li> {{/each}} </ul> <button {{action add}}>Add Item</button></script>

Page 11: Introduction to Ember

DEMO

Page 12: Introduction to Ember

Thanks!