Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Events & Actions in Ember.js

Luke Melia
September 12, 2013

Events & Actions in Ember.js

Understanding events & actions (and the differences between them) will help you build cleaner, more maintainable Ember.js apps.

Luke Melia

September 12, 2013
Tweet

More Decks by Luke Melia

Other Decks in Technology

Transcript

  1. Understanding events and actions in Ember will help your build

    maintainable apps. 7 Thursday, September 19, 13
  2. Understanding events and actions in Ember will help your build

    maintainable apps. 7 Let’s dig in. Thursday, September 19, 13
  3. 11 Actions Example: Initiated on click of a person in

    a list... Thursday, September 19, 13
  4. General benefits of both actions & events ★ Uni-directional ★

    No need to be aware of collaborators ★ Easy refactoring ★ “Loose coupling” 14 Thursday, September 19, 13
  5. Events are like a Town Crier. “Hear ye, hear ye,

    the new mayor of Provincetown is Brian Cardarella!” 15 Thursday, September 19, 13
  6. Ember’s Event Primitives removeListener 22 removes all listeners if no

    function is specified Thursday, September 19, 13
  7. DOM Events in Ember 31 ▪EventDispatcher sets up event delegation

    for many common DOM events and triggers events on the appropriate Ember View. ▪Event names roughly match DOM event names. ▪Views can optionally have an `eventManager` Thursday, September 19, 13
  8. The More You Know™ 32 ▪Events can be triggered with

    optional arguments, and listeners will receive them ▪Ember.Evented#has lets you check to see if there are any listeners for an event name. Thursday, September 19, 13
  9. Actions in Depth 33 ★Actions helper ★General flow ★Actions helper

    options & limitations ★Actions hash semantics ★The Router and events Thursday, September 19, 13
  10. 35 At a high level... Template/ View Controller Router action

    action Template/ View Target action Thursday, September 19, 13
  11. 35 At a high level... Template/ View Controller Router action

    action Template/ View Target action Controller Target action Thursday, September 19, 13
  12. Limitation: multiple action helpers 38 Not Supported Note: This should

    become possible in a future version of Ember via HTMLBars Thursday, September 19, 13
  13. Controller targets ▪ By default, the router is injected as

    `target` ▪ Sometimes, Ember sets a controller’s `target` is set to another controller ▪ {{render}} sets rendered controller to the template’s `controller` ▪ {{each}} with an itemController, itemController instances `targets` are set to the parent 39 Thursday, September 19, 13
  14. 42 The Ember Router is a state machine ▪ State

    machines are a proven way to reduce complexity in apps ▪ External components send actions to a state machine without knowing what state it is in – the current state will handle it ▪ Similar win as polymorphism – very decoupled Thursday, September 19, 13
  15. 43 The Ember Router is a state machine Template Router

    action Thursday, September 19, 13
  16. 43 The Ember Router is a state machine Template Router

    Current Route action action Thursday, September 19, 13
  17. 43 The Ember Router is a state machine Template Router

    Current Route action action Template Router Product Route logout logout OrderConfirmation Route vs. Thursday, September 19, 13
  18. 44 The Ember Router is a hierarchical state machine ▪

    Hierarchical state machines allow better modeling of common event handling ▪ Actions start at the current route and flow up to parent routes until a route handles it ▪ The router provides a natural hierarchy Thursday, September 19, 13
  19. 46 The Ember Router is a hierarchical state machine ApplicationRoute

    ProductRoute OrderRoute ShippingRoute BillingRoute ConfirmationRoute Thursday, September 19, 13
  20. 47 The Ember Router is a hierarchical state machine ApplicationRoute

    ProductRoute OrderRoute ShippingRoute BillingRoute ConfirmationRoute ApplicationRoute OrderRoute BillingRoute Thursday, September 19, 13
  21. 47 The Ember Router is a hierarchical state machine ApplicationRoute

    ProductRoute OrderRoute ShippingRoute BillingRoute ConfirmationRoute Thursday, September 19, 13
  22. The More You Know 48 ▪ Return true from an

    action handler to keep the action bubbling ▪ Power move: use a stateManager for an action target Thursday, September 19, 13
  23. The More You Know 49 ▪ Mixin Ember.TargetActionSupport to give

    an object the ability to trigger an action. (Use ViewTargetActionSupport for views.) ▪ Mixin Ember.ActionHandler to give an object the ability to be a target. Thursday, September 19, 13
  24. 52 Thanks to awesome Flickr people http://www.flickr.com/photos/69294818@N07/8665563301 http://www.flickr.com/photos/69294818@N07/8567914425 http://www.flickr.com/photos/louish/5365383945 http://www.flickr.com/photos/69294818@N07/8613982513

    http://www.flickr.com/photos/boston_public_library/2381603470 http://www.flickr.com/photos/universalpops/5818737203 http://www.flickr.com/photos/zerega/1366292835 http://www.flickr.com/photos/zerega/1366292835 http://www.flickr.com/photos/69294818@N07/8567623655 http://www.flickr.com/photos/atwestern/9198331693 http://www.flickr.com/photos/alanrmiles/3405474639 http://www.flickr.com/photos/onesevenone/4051045279 http://www.flickr.com/photos/digiart2001/3611238793 http://www.flickr.com/photos/posterboynyc/8737852416 http://www.flickr.com/photos/dcreeves2000/114712202 http://www.flickr.com/photos/23912576@N05/2883743786 Thursday, September 19, 13
  25. 53 Follow me @lukemelia Luke and Yapp Labs are available

    for Ember.js-related consulting and training. Contact [email protected]. Q & A Thursday, September 19, 13