From 5a0d0b7fa043678d09409e3b0a7a9d541b65663f Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 5 Mar 2012 22:44:58 +0100 Subject: Added basic playlist implementation using backbone.js. --- static/playlist.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 static/playlist.js (limited to 'static/playlist.js') 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('{{trackname}}'), + 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; +}); -- cgit v1.2.3