a live polling system in ember.js

12

Click here to load reader

Upload: yoranbe

Post on 17-May-2015

703 views

Category:

Technology


3 download

DESCRIPTION

"A Live Polling System in Ember.js" presented at the Boston Ember.js meetup on the 13th of February 2014 (http://www.meetup.com/Boston-Ember-js/events/159835752/)

TRANSCRIPT

Page 1: A Live Polling System in Ember.js

Live polling system in Ember.js

@YoranBrondsema

Page 2: A Live Polling System in Ember.js

About YoranMoved to Boston 5 days ago

CTO of Hstry (hiring btw!)Organizer of Brussels Ember.js meetups

Page 3: A Live Polling System in Ember.js

Live polling systemFor periodic updates in a single-page app

No real-time updatesUsed in Hstry because user stays for long time on

same Route

Page 4: A Live Polling System in Ember.js

How does Ember help?Add models to the store and templates update

themselves

Page 5: A Live Polling System in Ember.js

ExampleWe have Post's and Comment's

We want to periodically update the new commentsto a post

Page 6: A Live Polling System in Ember.js

Show me the codePollster object

Responsible for setting an interval that periodicallycalls an onPoll function

App.Pollster = Ember.Object.extend({ start: function() { // POLL_INTERVAL is for instance 30 seconds this.timer = setInterval(this.onPoll.bind(this), POLL_INTERVAL); }, stop: function() { clearInterval(this.timer); }, onPoll: function() { // Issue JSON request and add data to the store }});

Page 7: A Live Polling System in Ember.js

Setup this pollster in the routeApp.PostRoute = Ember.Route.extend({ setupController: function(controller, model) { if (Ember.isNone(this.get('pollster'))) { this.set('pollster', App.Polster.create({ onPoll: function() { // Defined on the next slide } })); } this.get('pollster').start(); }, // This is called upon exiting the Route deactivate: function() { this.get('pollster').stop(); }});

Page 8: A Live Polling System in Ember.js

The onPoll function// This code is executed in the 'setupController' hook of the Routevar route = this;

onPoll: function() { // AJAX request Ember.$.getJSON('/updates', 'GET').then(function(json_obj) { // The JSON structure is as follows: // { // comments: [ // { ... }, // { ... }, // ] // }

// Iterate through the comments (NOTE: ̀Ember.forEach̀ cross-browser compatible?) Ember.$.each(json_obj.comments, function(index, comment) { // Make sure that the comment is not already in the store if (! route.get('store').recordIsLoaded(App.Comment, comment.id)) { route.get('store').push('comment', comment); } }); });}

Page 9: A Live Polling System in Ember.js

Now we have added the data to the storeBut the view is not necessarily updatedNeed to use the filter API function

Filters, on the other hand, perform a live search of all of therecords in the store’s cache.

Takes a type and filter function, and returns a live RecordArraythat remains up to date as new records are loaded into the

store or created locally.

Page 10: A Live Polling System in Ember.js

Our template<article class="post"> <h2>{{title}}</h2> <p>{{content}}</p> <aside>{{render "comments" comments}}</aside></article>

Page 11: A Live Polling System in Ember.js

Our controller for the templateApp.PostController = Ember.ObjectController.extend({ comments: function() { var postId = this.get('id'); // Get all the comments that belong to this post return this.get('store').filter('comment', function(comment) { return comment.get('post.id') == postId; }); }.property()});

Each Comment added to the Store is automaticallyshown

Page 12: A Live Polling System in Ember.js

Questions?Based on yoranbrondsema.com/live-polling-system-ember-js

@YoranBrondsema

[email protected]