diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2012-03-05 22:44:58 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2012-03-05 22:44:58 +0100 |
commit | 5a0d0b7fa043678d09409e3b0a7a9d541b65663f (patch) | |
tree | 1c6c9e633fc1866ac05578bc08d2845f3c40a230 /static/playlist.js | |
parent | 39e3b4c0822780310af8dd9b0f9dd89225be3b5c (diff) |
Added basic playlist implementation using backbone.js.
Diffstat (limited to 'static/playlist.js')
-rw-r--r-- | static/playlist.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/static/playlist.js b/static/playlist.js new file mode 100644 index 0000000..1de78fa --- /dev/null +++ b/static/playlist.js @@ -0,0 +1,57 @@ +$(function(){ + PlaylistItem = Backbone.Model.extend({}); + Playlist = Backbone.Collection.extend({ + model: PlaylistItem, + localStorage: new Store("playlist") + }); + window.items = new Playlist; + PlaylistItemView = Backbone.View.extend({ + tagName: 'li', + template: Handlebars.compile('<a href="#">{{trackname}}</a>'), + render: function() { + var model = this.model; + var item = model.toJSON(); + var el = $(this.el); + el.html(this.template(item)); + el.attr('id', 'cid-' + model.cid); + $(this.el, 'a').click(function() { + while(model.collection.indexOf(model) > 0) { + model.collection.remove(model.collection.at(0)); + } + playsound(model); + return false; + }); + return this; + } + }); + PlaylistView = Backbone.View.extend({ + el: $('#playlist'), + initialize: function() { + items.bind('add', this.addOne, this); + items.bind('remove', this.removeOne, this); + this.current = null; + }, + add: function(item) { + var model = items.create(item); + }, + addOne: function(item) { + var view = new PlaylistItemView({model: item}); + var el = view.render().el; + if(item.attributes.nocache) + $(el).addClass('nocache'); + $('#playlist').append(el); + }, + removeOne: function(item) { + $('#playlist #cid-' + item.cid).remove(); + }, + next: function() { + var item = items.at(0); + var next = items.at(1); + items.remove(item); + this.render(); + return next; + } + }); + + window.playlist = new PlaylistView; +}); |