1
0
mirror of https://github.com/danbee/mpd-client synced 2025-03-04 08:39:09 +00:00

Refactor server into construct.

This commit is contained in:
Dan Barber 2014-01-08 15:01:59 +00:00
parent 7de06dcea8
commit 2d8207ece4
6 changed files with 55 additions and 29 deletions

View File

@ -2,6 +2,13 @@ can.Component.extend({
tag: 'mpd-queue', tag: 'mpd-queue',
template: can.view('views/queue.mustache') template: can.view('views/queue.mustache'),
events: {
'li click': function(element, event) {
var pos = $(element).data('pos');
server.playSong(pos);
}
}
}); });

View File

@ -5,16 +5,9 @@ can.Component.extend({
template: can.view('views/transport.mustache'), template: can.view('views/transport.mustache'),
events: { events: {
sendCommand: function(command) {
can.ajax({
url: '/api/control/' + command,
type: 'PUT'
});
},
'button click': function(element, event) { 'button click': function(element, event) {
var command = $(element).data('command'); var command = $(element).data('command');
this.sendCommand(command); server.sendCommand(command);
$(element).blur(); $(element).blur();
} }
} }

View File

@ -0,0 +1,31 @@
var Server = can.Construct.extend({
init: function() {
this.eventSource = new EventSource('/api/stream')
},
onMessage: function(callback) {
this.eventSource.addEventListener('message', function(event) {
var response = JSON.parse(event.data);
callback(response);
});
},
sendCommand: function(command) {
can.ajax({
url: '/api/control/' + command,
type: 'PUT'
});
},
playSong: function(pos) {
can.ajax({
url: '/api/control/play',
data: { pos: pos },
type: 'PUT'
});
}
});
window.server = new Server();

View File

@ -1,5 +0,0 @@
window.server = {
eventSource: new EventSource('/api/stream'),
}

View File

@ -13,11 +13,10 @@ var QueueSong = can.Model.extend({
QueueSong.List = can.List.extend({ QueueSong.List = can.List.extend({
init: function() { init: function() {
server.eventSource.addEventListener('message', this.updateQueue.bind(this)); server.onMessage(this.updateQueue.bind(this));
}, },
updateQueue: function(event) { updateQueue: function(response) {
var response = JSON.parse(event.data);
if (response.type === 'queue') { if (response.type === 'queue') {
this.attr(response.data, true); this.attr(response.data, true);
} }
@ -27,7 +26,9 @@ QueueSong.List = can.List.extend({
this.each(function(item) { this.each(function(item) {
item.attr('playing', false); item.attr('playing', false);
}); });
if (this.attr(newSong) !== undefined) {
this.attr(newSong).attr('playing', true); this.attr(newSong).attr('playing', true);
} }
}
}); });

View File

@ -5,11 +5,10 @@ var Status = can.Model.extend({
}, { }, {
init: function() { init: function() {
server.eventSource.addEventListener('message', this.updateStatus.bind(this)); server.onMessage(this.updateStatus.bind(this));
}, },
updateStatus: function(event) { updateStatus: function(response) {
var response = JSON.parse(event.data);
switch (response.type) { switch (response.type) {
case 'status': case 'status':
this.attr(response.data); this.attr(response.data);