2013-05-05 23:24:33 +02:00
|
|
|
"use strict";
|
|
|
|
|
2013-05-06 03:16:38 +02:00
|
|
|
_.extend(Backbone.Collection.prototype, {BindSignalR: function (options) {
|
2013-05-06 02:33:43 +02:00
|
|
|
|
|
|
|
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-05 23:24:33 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-06 02:33:43 +02:00
|
|
|
var connection = $.connection(options.url);
|
2013-05-05 23:24:33 +02:00
|
|
|
|
2013-05-06 02:33:43 +02:00
|
|
|
connection.stateChanged(function (change) {
|
|
|
|
console.debug('{0} [{1}]'.format(options.url, _getStatus(change.newState)));
|
2013-05-05 23:24:33 +02:00
|
|
|
});
|
|
|
|
|
2013-05-06 03:16:38 +02:00
|
|
|
connection.received(function (model) {
|
|
|
|
console.debug(model);
|
2013-05-06 02:33:43 +02:00
|
|
|
self.fetch();
|
2013-05-05 23:24:33 +02:00
|
|
|
});
|
|
|
|
|
2013-05-06 02:33:43 +02:00
|
|
|
connection.start({ transport: ['longPolling'] });
|
|
|
|
|
|
|
|
return this;
|
2013-05-06 03:16:38 +02:00
|
|
|
}});
|
2013-05-06 02:33:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|