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

Integrating Ember into existing sites

crofty
February 13, 2014

Integrating Ember into existing sites

Ember is targeting itself at bring a framework got building JS only web apps. At its core though, Ember has a great object model that can be used even when the whole site is not built using Ember.

I'll walk through a site (http://endlessrotation.com) which isn't a good fit to be a JS only web app, and show how Ember can be used to build just the bits that make sense.

crofty

February 13, 2014
Tweet

More Decks by crofty

Other Decks in Technology

Transcript

  1. 1 {{track-player track=controllers.track}} 1 App.TrackPlayerComponent = Ember.Component.extend 2 setupPlayer: (

    -> 3 player = new YT.Player @get('element') 4 @set('player', player) 5 ).on('didInsertElement') 6 7 playVideo: ( -> 8 @get('player').loadVideoById @get('track.videoId') 9 ).observes('track.videoId') 10 11 App.TrackController = Ember.ObjectController.extend()
  2. 1 App.ApplicationRoute = Ember.Route.extend 2 actions: 3 playTrack: (track) ->

    4 @controllerFor('track').set('content', track) 5 1 <ul> 2 {{#each track in controllers.tracks}} 3 <li> 4 <a {{action playTrack track}}> ▶ </a> 5 {{track.title}} 6 </li> 7 {{/each}} 8 </ul>
  3. 1 $('body').on 'click', 'a.play', (e) -> 2 $link = $(this)

    3 artist = $link.data('artist') 4 title = $link.data('title') 5 videoId = # Search YouTube for the video 6 7 track = Ember.Object.create 8 artist: artist 9 title: title 10 videoId: videoId 11 12 # Get the track into the application 1 <li> 2 <a class="play" data-artist="Andhim" data-title="Reeves"> ▶ </a> 3 Andhim - Reeves 4 </li>
  4. 1 $('body').on 'click', 'a.play', (e) -> 2 $link = $(this)

    3 artist = $link.data('artist') 4 title = $link.data('title') 5 videoId = # Search YouTube for the video 6 7 track = Ember.Object.create 8 artist: artist 9 title: title 10 videoId: videoId 11 12 trackController = App.__container__.lookup('controller:track') 13 trackController.set('content', track) 1 <li> 2 <a class="play" data-artist="Andhim" data-title="Reeves"> ▶ </a> 3 Andhim - Reeves 4 </li>
  5. 1 2 3 4 5 6 $('body').on 'click', 'a.play', (e)

    -> 7 $link = $(this) 8 artist = $link.data('artist') 9 title = $link.data('title') 10 videoId = # Search YouTube for the video 11 12 track = Ember.Object.create 13 artist: artist 14 title: title 15 videoId: videoId 16 17
  6. 1 App.initializer 2 name: "setup-play-link-handler", 3 initialize: (container, application) ->

    4 trackController = container.lookup('controller:track') 5 6 $('body').on 'click', 'a.play', (e) -> 7 $link = $(this) 8 artist = $link.data('artist') 9 title = $link.data('title') 10 videoId = # Search YouTube for the video 11 12 track = Ember.Object.create 13 artist: artist 14 title: title 15 videoId: videoId 16 17 trackController.set('content', track)