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',
|
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'),
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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({
|
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);
|
||||||
});
|
});
|
||||||
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() {
|
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);
|
break;
|
||||||
break;
|
case 'time':
|
||||||
case 'time':
|
this.attr('time', response.data);
|
||||||
this.attr('time', response.data);
|
break;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
playing: function() {
|
playing: function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user