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

Ember.js State Manager

Ember.js State Manager

An overview of the state manager in Ember.js and how it could be applied to Riak Control.

Christopher Meiklejohn

June 28, 2012
Tweet

More Decks by Christopher Meiklejohn

Other Decks in Programming

Transcript

  1. Who Am I? • Christopher Meiklejohn • Software Engineer at

    Basho Technologies • JavaScript, JavaScript and JavaScript! Thursday, June 28, 12
  2. • Monitoring application bundled with Riak. • Open source. •

    PJAX, jQuery, Webmachine. Riak Control Thursday, June 28, 12
  3. PJAX • Unconventional PJAX usage. • Loads template into DOM.

    • Callback appends script to page. • Script self-invoking to retrieve data from server, and render data into page. Thursday, June 28, 12
  4. PJAX • All state is stored in the DOM. •

    Rendered from hidden templates, also in the DOM. • Page is redrawn whenever displayed. Thursday, June 28, 12
  5. PJAX • Usually used to return HTML templates with the

    data already populated. • Slow connections also problematic. • Page will be redirected to if AJAX request doesn’t return in time. • Empty template. • Not good. Thursday, June 28, 12
  6. State Management • Pub/Sub implementation. • When tabs are clicked,

    event is triggered to swap template. • PJAX loaded, self invoking JavaScript subscribes to these events. Thursday, June 28, 12
  7. State Management • Very problematic when trying to manage state

    between tabs. • Each section is redrawn completely when navigated to. Thursday, June 28, 12
  8. State Management • Also controls polling. • When template is

    loaded for first time, starts polling loop. • Only retrieves data if template is displayed. • Polling loop for each tab. • Unnecessary and expensive. Thursday, June 28, 12
  9. Data Bindings • No reason for individual pages to load

    duplicate information. • Load information into objects, and share between the sections. • Views updated automatically. • One polling loop. Thursday, June 28, 12
  10. Run Loop • Batch updates to the DOM. • Only

    re-render sections which have changed. Thursday, June 28, 12
  11. Templating • Handlebars templates. • No longer have to hide

    elements in the DOM, loop over, and clone into the page. Thursday, June 28, 12
  12. State Manager • Implementation of a finite state machine. •

    Define a series of application states. • Based on state, can be used to: • Control visible elements on page. • Control polling based on selection. Thursday, June 28, 12
  13. What The Router Solved. • ViewStates had no handling for

    serialization of state via the URL. • Nested ViewStates only appended objects to the DOM, couldn’t control insertion point. • More not listed here, see: http:// tomdale.net/2012/05/ember-routing/ Thursday, June 28, 12
  14. The Router • Router is a type of StateMachine. •

    Define a series of routes, which are just states with an additional route property. • Route property used to build URL. • URL serializes the current state of your application. Thursday, June 28, 12
  15. The Application • Router requires: • an ApplicationController • an

    ApplicationView • an application.handlebars • App.initialize(App.Router) • If you use rails, ember-rails generator handles this. Thursday, June 28, 12
  16. Outlets • Defined in your templates. • Area for a

    child template, determined at runtime. Thursday, June 28, 12
  17. Outlets, Lots of Magic • connectOutlet(name, context) • Names are

    inferred, for example: • snapshot -> App.SnapshotView. • snapshot -> App.SnapshotController • Controller and view are instantiated. • context is bound to content on the controller. Thursday, June 28, 12
  18. Outlets Sound familiar? Same as when the application and router

    were initialized. Thursday, June 28, 12
  19. Riak Control Polling • Use Ember Data to store all

    objects in application data store. • Inspect current state to determine which objects to poll and update. • Cleanly isolates XHR polling loop outside of view/templating so it can be switched to a more robust solution (ie. Comet, WebSockets.) Thursday, June 28, 12
  20. Riak Control Templating • Handlebars templates loaded immediately. • Templates

    used to render the objects into the DOM. • Views are bound directly to objects. • Views are no longer redrawn every < 1s. Thursday, June 28, 12
  21. Final Points • Ember Data: Very young, very opinionated. •

    Ember Data: Lots of things don’t work yet. • Ember Router built around using Ember Data; assumption that you’ll have objects returned immediately that are loaded async. • Ember Router: not officially recommended for production, at least until 1.0. Thursday, June 28, 12