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

Ember & Firebase

Ember & Firebase

A talk about Ember.js and Firebase (through EmberFire)
Given at EmberLondon on Wednesday 13th November 2013

Matthew Rudy Jacobs

November 13, 2013
Tweet

More Decks by Matthew Rudy Jacobs

Other Decks in Technology

Transcript

  1. me

  2. Meteor • Pure Javascript • Live page updates • Clean,

    powerful data synchronisation • Latency compensation • Hot Code Pushes • Sensitive code runs in a privileged environment • Fully self-contained application bundles • Interoperability • Smart Packages
  3. Meteor • Pure Javascript • Live page updates • Clean,

    powerful data synchronisation • Latency compensation • Hot Code Pushes • Sensitive code runs in a privileged environment • Fully self-contained application bundles • Interoperability • Smart Packages
  4. uri = “https://example.firebaseio.com/players” ref = new Firebase uri ! keys

    = []; players = [] ! ref.on ‘child_added’, (snapshot) -> keys.push snapshot.name() players.push snapshot.val()
  5. Albert ref.push name: “Albert Einstein” score: 0 ! # ref.on

    ‘child_added’, # (snapshot) -> # keys.push snapshot.name() # players.push snapshot.val()
  6. EmberFire.Object EmberFire.Object = Ember.ObjectProxy.extend init: -> @set ‘content’, {} @ref.on

    ‘value’, (snapshot) -> @set ‘content’, snapshot.val() , @ ! setUnknownProperty: (key, value) -> @ref.child(key).set(value)
  7. EmberFire.ObjectArray EmberFire.Ember.ArrayProxy.extend init: -> @set ‘content’, Ember.A([]) @set ‘keys’, Ember.A([])

    ! @ref.on “child_added”, (snapshot) -> object = EmberFire.Object.create ref: snapshot.ref() @get(‘content’).pushObject object @get(‘keys’).pushObject snapshot.name() ! @ref.on “child_removed”, (snapshot) -> idx = @get(‘keys’).indexOf(snapshot.name()) @get(‘content’).removeAt idx @get(‘keys’).removeAt idx
  8. Use it uri = “https://example.firebaseio.com/players” ! playerList = EmberFire.ObjectArray.create ref:

    new Firebase uri ! playerList.mapProperty(‘name') ! # ["Claude Shannon”, "Nikola Tesla", # "Carl Friedrich Gauss”, ”Marie Curie”, # ”Grace Hopper", "Ada Lovelace"]
  9. Use it ada = playerList.get ‘lastObject’ ada.incrementProperty ‘score’, 10 !

    playerList.pushObject name: “Albert Einstein” score: 0
  10. Meteor Players = new Meteor.Collection “players” ! if Meteor.isClient Template.leaderboard.players

    = -> Players.find {}, sort: score: -1 name: 1 ! <div class="leaderboard"> {{#each players}} {{> player}} {{/each}} </div>
  11. ember-meteor App.LeaderboardRoute = Ember.Route.extend model: -> @get(‘store’).find('player') ! App.LeaderboardController =

    Ember.ArrayController.extend playerSorting: ["score:desc", "name:asc"] players: Ember.computed.sort('content', ‘playerSorting') ! <div class="leaderboard"> {{#each player in players}} {{render 'player' player}} {{/each}} </div>
  12. ember-meteor + emberfire App.PlayerList = EmberFire.ObjectArray.extend firebaseURI: "https://ember-meteor-leaderboard.firebaseio.com/players" ! init:

    -> firebase = new Firebase @get(‘firebaseURI’) @set ‘ref', firebase @_super() ! App.LeaderboardRoute = Ember.Route.extend model: -> App.PlayerList.create() ! App.LeaderboardController = Ember.ArrayController.extend players: (-> Ember.ArrayProxy.createWithMixins Ember.SortableMixin, content: @get(‘content') sortProperties: ['score'] sortAscending: false ).property(‘content’) ! <div class="leaderboard"> {{#each player in players}} {{render 'player' player}} {{/each}} </div>