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

Hijacking Hacker News with Ember.js

Hijacking Hacker News with Ember.js

EmberConf 2015

Godfrey Chan

March 03, 2015
Tweet

More Decks by Godfrey Chan

Other Decks in Programming

Transcript

  1. BEFORE AFTER image courtesy of TOM DALE “THE FACE OF

    OUR NATION” Wednesday, 4 March, 15
  2. 52 °F 52 °Freedom 11 °CANADA 11 °cDEGREES OF FREEDOM

    “It is currently 11 degrees, in canada.” Wednesday, 4 March, 15
  3. var isEmpty = false; var isLoaded = true; var emptyEh

    = false; var loadedEh = true; Wednesday, 4 March, 15
  4. >> require 'canada' => true >> [].empty_eh? => true >>

    [1,2,3].empty_eh? => false Wednesday, 4 March, 15
  5. function FreedomMonitor(sensor) { setInterval(function() { // Expects °F if (sensor.readTemperature()

    > 100) { ... } }, 1000); }; var CanadianSensor = { getTemperature: function() { // Measures temperature in °C } }; Wednesday, 4 March, 15
  6. function CanadianAdapter(sensor) { this.sensor = sensor; }; CanadianAdapter.prototype = {

    readTemperature: function() { return this.sensor.getTemperature() * 1.8 + 32; } }; // Use it! new FreedomMonitor( new CanadianAdapter(CanadianSensor) ); Wednesday, 4 March, 15
  7. $.get("/news").then( function(html) { var stories = []; $(html).find("tr .title a").each(

    function (_, $link) { stories.push({ title: $link.text(), url: $link.attr('href') }); }); ... return stories; }); Wednesday, 4 March, 15
  8. Ember Data ( DATA STORE ) JSON API ( DATA

    SOURCE ) Wednesday, 4 March, 15
  9. Ember Data ( DATA STORE ) HACKER NEWS HTML SCRAPPER

    ( DATA SOURCE ) Wednesday, 4 March, 15
  10. Ember Data ( DATA STORE ) HACKER NEWS HTML SCRAPPER

    ( DATA SOURCE ) Wednesday, 4 March, 15
  11. App.StoryAdapter = DS.Adapter.extend({ findAll(store, type, id) { return $.get("/news"); }

    }); App.StorySerializer = DS.Serializer.extend({ extractArray(store, type, payload) { var stories = []; $(payload).find("tr .title a").each( ... ); return stories; } }); Wednesday, 4 March, 15
  12. Ember ROUTER ( ACTUAL APP STATES ) HACKER NEWS URLS

    ( SERIALIZED APP STATES ) Wednesday, 4 March, 15
  13. Ember ROUTER ( ACTUAL APP STATES ) HACKER NEWS URLS

    ( SERIALIZED APP STATES ) Wednesday, 4 March, 15
  14. App.HackerNewsLocation = Ember.HistoryLocation.extend({ getURL: function() { if (this._super() === "/news")

    { return "/stories"; } else if (...) { ... } }, formatURL: function( logicalPath ) { if (logicalPath === "/stories") { return "/news"; } else if (...) { ... } } }); App.Router.reopen({ location: 'hacker-news' }); Wednesday, 4 March, 15
  15. App.LocalStorage = Ember.Object.extend({ init: function() { $(window).on("storage", Ember.run.bind(this, "_onStorageEvent") );

    }, unknownProperty: function(key) { return localStorage.getItem(key); }, setUnknownProperty: function(key, value) { localStorage.setItem(key, value); return true; }, _onStorageEvent: function(e) { this.notifyPropertyChange(e.key); }, willDestroy: function() { $(window).off("storage"); } }); Wednesday, 4 March, 15
  16. $ cd cocoabars $ git status # On branch master

    # # Initial commit # nothing to commit (create/copy files and use "git add" to track) Wednesday, 4 March, 15
  17. A few awesome things didn’t make it to the slides.

    I’ll be tweeting, stay tuned. Wednesday, 4 March, 15
  18. I’d like to dedicate this talk to my favourite teacher

    and life-mentor, Miss Shirley Ngai. Wednesday, 4 March, 15
  19. And of course, thank you Ember contributors for making this

    amazing framework. Wednesday, 4 March, 15
  20. Huge thanks to those who beta tested my app and

    tolerated the “dry” runs of my talk. Wednesday, 4 March, 15
  21. And finally, thank YOU for sitting through my first-ever conference

    talk and being awesome! Wednesday, 4 March, 15