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:
parent
7de06dcea8
commit
2d8207ece4
@ -2,6 +2,13 @@ can.Component.extend({
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@ -5,16 +5,9 @@ can.Component.extend({
|
||||
template: can.view('views/transport.mustache'),
|
||||
|
||||
events: {
|
||||
sendCommand: function(command) {
|
||||
can.ajax({
|
||||
url: '/api/control/' + command,
|
||||
type: 'PUT'
|
||||
});
|
||||
},
|
||||
|
||||
'button click': function(element, event) {
|
||||
var command = $(element).data('command');
|
||||
this.sendCommand(command);
|
||||
server.sendCommand(command);
|
||||
$(element).blur();
|
||||
}
|
||||
}
|
||||
|
||||
31
assets/js/constructs/server.js
Normal file
31
assets/js/constructs/server.js
Normal 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();
|
||||
@ -1,5 +0,0 @@
|
||||
window.server = {
|
||||
|
||||
eventSource: new EventSource('/api/stream'),
|
||||
|
||||
}
|
||||
@ -13,11 +13,10 @@ var QueueSong = can.Model.extend({
|
||||
QueueSong.List = can.List.extend({
|
||||
|
||||
init: function() {
|
||||
server.eventSource.addEventListener('message', this.updateQueue.bind(this));
|
||||
server.onMessage(this.updateQueue.bind(this));
|
||||
},
|
||||
|
||||
updateQueue: function(event) {
|
||||
var response = JSON.parse(event.data);
|
||||
updateQueue: function(response) {
|
||||
if (response.type === 'queue') {
|
||||
this.attr(response.data, true);
|
||||
}
|
||||
@ -27,7 +26,9 @@ QueueSong.List = can.List.extend({
|
||||
this.each(function(item) {
|
||||
item.attr('playing', false);
|
||||
});
|
||||
this.attr(newSong).attr('playing', true);
|
||||
if (this.attr(newSong) !== undefined) {
|
||||
this.attr(newSong).attr('playing', true);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@ -5,19 +5,18 @@ var Status = can.Model.extend({
|
||||
}, {
|
||||
|
||||
init: function() {
|
||||
server.eventSource.addEventListener('message', this.updateStatus.bind(this));
|
||||
server.onMessage(this.updateStatus.bind(this));
|
||||
},
|
||||
|
||||
updateStatus: function(event) {
|
||||
var response = JSON.parse(event.data);
|
||||
switch (response.type) {
|
||||
case 'status':
|
||||
this.attr(response.data);
|
||||
break;
|
||||
case 'time':
|
||||
this.attr('time', response.data);
|
||||
break;
|
||||
}
|
||||
updateStatus: function(response) {
|
||||
switch (response.type) {
|
||||
case 'status':
|
||||
this.attr(response.data);
|
||||
break;
|
||||
case 'time':
|
||||
this.attr('time', response.data);
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
playing: function() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user