Created
November 5, 2011 02:14
-
-
Save ludicast/1340993 to your computer and use it in GitHub Desktop.
translation into coffeescript of code for backbone.js peepcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.Album = Backbone.Model.extend | |
isFirstTrack: (index)-> | |
index == 0 | |
isLastTrack: (index)-> | |
index >= @get('tracks').length - 1 | |
trackUrlAtIndex: (index)-> | |
if @get('tracks').length > index | |
@get('tracks')[index].url | |
else | |
null | |
window.Albums = Backbone.Collection.extend | |
model: Album | |
url: "/albums" | |
window.Playlist = Albums.extend | |
isFirstAlbum: (index)-> index is 0 | |
isLastAlbum: (index)-> index is @models.length - 1 | |
window.Player = Backbone.Model.extend | |
defaults: { | |
currentAlbumIndex: 0 | |
currentTrackIndex: 0 | |
state: 'stop' | |
} | |
reset: -> | |
@set @defaults | |
initialize: -> | |
@playlist = new Playlist() | |
play: -> | |
@set state:'play' | |
@trigger 'change:currentTrackIndex' | |
@logCurrentAlbumAndTrack() | |
pause: -> @set state:'pause' | |
isPlaying: -> @get('state') is 'play' | |
isStopped: -> !@isPlaying() | |
currentAlbum: -> @playlist.at @get('currentAlbumIndex') | |
currentTrackUrl: -> | |
album = @currentAlbum() | |
album?.trackUrlAtIndex(@get 'currentTrackIndex') | |
nextTrack: -> | |
trackIndex = @get 'currentTrackIndex' | |
albumIndex = @get 'currentAlbumIndex' | |
if @currentAlbum().isLastTrack(trackIndex) | |
if @playlist.isLastAlbum albumIndex | |
@set | |
currentAlbumIndex: 0 | |
currentTrackIndex: 0 | |
else | |
@set | |
currentAlbumIndex:(albumIndex + 1) | |
currentTrackIndex: 0 | |
else | |
@set currentTrackIndex:(trackIndex + 1) | |
@logCurrentAlbumAndTrack() | |
prevTrack: -> | |
trackIndex = @get 'currentTrackIndex' | |
albumIndex = @get 'currentAlbumIndex' | |
if @currentAlbum().isFirstTrack(trackIndex) | |
if @playlist.isFirstAlbum albumIndex | |
lastModelIndex = @playlist.models.length - 1 | |
@set currentAlbumIndex: lastModelIndex | |
else | |
@set currentAlbumIndex:(albumIndex - 1) | |
lastTrackIndex = @currentAlbum().get('tracks').length - 1 | |
@set currentTrackIndex:lastTrackIndex | |
else | |
@set currentTrackIndex:(trackIndex - 1) | |
@logCurrentAlbumAndTrack() | |
logCurrentAlbumAndTrack: -> | |
console.log "Player " + | |
"#{@get('currentAlbumIndex')}:#{@get('currentTrackIndex')}", @ | |
window.library = new Albums() | |
window.player = new Player() | |
$ -> | |
window.AlbumView = Backbone.View.extend | |
tag: 'li' | |
className: 'album' | |
template: _.template $("#album-template").html() | |
initialize: -> | |
_.bindAll(this, 'render') | |
render: -> | |
model = @model.toJSON() | |
renderedContent = @template(model) | |
$(this.el).html(renderedContent) | |
@ | |
window.PlaylistAlbumView = AlbumView.extend | |
events: { | |
'click .queue.remove': 'removeFromPlaylist' | |
} | |
initialize: -> | |
_.bindAll(@, 'render', 'updateState', 'updateTrack', 'remove') | |
@player = @options.player | |
@player.bind 'change:state', @updateState | |
@player.bind 'change:currentTrackIndex', @updateTrack | |
@model.bind 'remove', @remove | |
render: -> | |
$(@el).html @template(@model.toJSON()) | |
@updateTrack() | |
@ | |
updateState: -> | |
isAlbumCurrent = @player.currentAlbum() is @model | |
$(@el).toggleClass 'current', isAlbumCurrent | |
updateTrack: -> | |
isAlbumCurrent = @player.currentAlbum() is @model | |
if isAlbumCurrent | |
currentTrackIndex = @player.get 'currentTrackIndex' | |
this.$('li').each (index, el)-> | |
$(el).toggleClass 'current', (index is currentTrackIndex) | |
@updateState() | |
removeFromPlaylist: -> | |
@options.playlist.remove(@model) | |
@player.reset() | |
window.LibraryAlbumView = AlbumView.extend | |
events: { | |
'click .queue.add': 'select' | |
} | |
select: -> | |
@collection.trigger('select', @model) | |
window.PlaylistView = Backbone.View.extend | |
tagName: 'section' | |
className: 'playlist' | |
template: _.template $("#playlist-template").html() | |
events: { | |
'click .play': 'play' | |
'click .pause': 'pause' | |
'click .next': 'nextTrack' | |
'click .prev': 'prevTrack' | |
} | |
initialize: -> | |
_.bindAll(this, 'render', 'renderAlbum', 'queueAlbum', 'updateState', 'updateTrack') | |
@collection.bind 'reset', @render | |
@collection.bind 'add', @renderAlbum | |
@player = @options.player | |
@player.bind 'change:state', @updateState | |
@player.bind 'change:currentTrackIndex', @updateTrack | |
@createAudio() | |
@library = @options.library | |
@library.bind 'select', @queueAlbum | |
createAudio: -> | |
@audio = new Audio() | |
render: -> | |
$(@el).html(@template(@player.toJSON())) | |
@collection.each @renderAlbum | |
@updateState() | |
@ | |
renderAlbum: (album)-> | |
view = new PlaylistAlbumView | |
model:album | |
player:@player | |
playlist:@collection | |
this.$('ul').append(view.render().el) | |
updateState: -> | |
@updateTrack() | |
this.$('button.play').toggle(@player.isStopped()) | |
this.$('button.pause').toggle(@player.isPlaying()) | |
updateTrack: -> | |
@audio.src = @player.currentTrackUrl() | |
if @player.get('state') == 'play' | |
@audio.play() | |
else | |
@audio.pause() | |
queueAlbum: (album)-> | |
@collection.add(album) | |
play: -> @player.play() | |
pause: -> @player.pause() | |
nextTrack: -> @player.nextTrack() | |
prevTrack: -> @player.prevTrack() | |
window.LibraryView = Backbone.View.extend | |
tagName: 'section' | |
className: 'library' | |
template: _.template $('#library-template').html() | |
initialize: -> | |
_.bindAll @, 'render' | |
@collection.bind 'reset', @render | |
render: -> | |
$(@el).html @template({}) | |
$albums = this.$('.albums') | |
@collection.each (album)=> | |
view = new LibraryAlbumView | |
model:album | |
collection:@collection | |
$albums.append view.render().el | |
@ | |
window.BackboneTunes = Backbone.Router.extend | |
routes: { | |
'': 'home' | |
'blank': 'blank' | |
} | |
initialize: -> | |
@playlistView = new PlaylistView | |
collection: window.player.playlist | |
player: window.player | |
library: window.library | |
@libraryView = new LibraryView | |
collection: window.library | |
home: -> | |
$container = $('#inner-container') | |
$container.empty() | |
$container.append(@playlistView.render().el) | |
$container.append(@libraryView.render().el) | |
blank: -> | |
$('#inner-container').empty() | |
$('#inner-container').text('blank') | |
window.App = new BackboneTunes() | |
Backbone.history.start() | |
window.library.fetch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment