2013-06-22 08:24:24 +02:00
|
|
|
'use strict';
|
2013-06-25 01:41:59 +02:00
|
|
|
define(
|
|
|
|
[
|
|
|
|
'signalR'
|
|
|
|
], function () {
|
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
_.extend(Backbone.Collection.prototype, {
|
|
|
|
bindSignalR: function (options) {
|
2013-06-25 01:41:59 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
2013-06-25 01:41:59 +02:00
|
|
|
}
|
2013-05-06 02:33:43 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
if (!options.url) {
|
|
|
|
console.assert(this.url, 'url must be provided or collection must have url');
|
|
|
|
options.url = this.url.replace('api', 'signalr');
|
|
|
|
}
|
2013-05-05 23:24:33 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.signalRconnection = $.connection(options.url);
|
|
|
|
|
|
|
|
this.signalRconnection.stateChanged(function (change) {
|
|
|
|
console.debug('{0} [{1}]'.format(options.url, _getStatus(change.newState)));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.signalRconnection.received(function (message) {
|
|
|
|
console.debug(message);
|
|
|
|
self.fetch();
|
|
|
|
});
|
2013-05-05 23:24:33 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
this.signalRconnection.start({ transport:
|
|
|
|
[
|
|
|
|
'longPolling'
|
|
|
|
] });
|
2013-08-10 20:38:01 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
return this;
|
|
|
|
},
|
2013-08-10 20:38:01 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
unbindSignalR: function () {
|
2013-08-10 20:38:01 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
if(this.signalRconnection){
|
|
|
|
this.signalRconnection.stop();
|
|
|
|
delete this.signalRconnection;
|
2013-08-10 20:38:01 +02:00
|
|
|
}
|
2013-05-06 02:33:43 +02:00
|
|
|
|
2013-08-21 21:07:28 +02:00
|
|
|
}});
|
2013-06-25 01:41:59 +02:00
|
|
|
});
|
2013-05-06 02:33:43 +02:00
|
|
|
|
|
|
|
|