1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/UI/Mixins/backbone.signalr.mixin.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2013-05-06 00:24:33 +03:00
"use strict";
Backbone.Collection.prototype.BindSignalR = function (options) {
if (!options || !options.url) {
console.assert(this.url, 'url must be provided or collection must have url');
options = {
url: this.url.replace('api', 'signalr')
};
}
var self = this;
2013-05-06 00:24:33 +03:00
var _getStatus = function (status) {
switch (status) {
case 0:
return 'connecting';
case 1:
return 'connected';
case 2:
return 'reconnecting';
case 4:
return 'disconnected';
default:
throw 'invalid status ' + status;
}
};
var connection = $.connection(options.url);
2013-05-06 00:24:33 +03:00
connection.stateChanged(function (change) {
console.debug('{0} [{1}]'.format(options.url, _getStatus(change.newState)));
2013-05-06 00:24:33 +03:00
});
connection.received(function () {
self.fetch();
2013-05-06 00:24:33 +03:00
});
connection.start({ transport: ['longPolling'] });
return this;
};