Bind any collection to SignalR with a single call.

This commit is contained in:
kay.one 2013-05-05 17:33:43 -07:00
commit a6aba16902
13 changed files with 106 additions and 26 deletions

View file

@ -1,7 +1,15 @@
"use strict";
(function ($) {
var connection = $.connection('/signalr/series');
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;
var _getStatus = function (status) {
switch (status) {
@ -19,18 +27,21 @@
};
var connection = $.connection(options.url);
connection.stateChanged(function (change) {
console.log('signalR [{0}]'.format(_getStatus(change.newState)));
console.debug('{0} [{1}]'.format(options.url, _getStatus(change.newState)));
});
connection.received(function (data) {
console.log(data);
connection.received(function () {
self.fetch();
});
connection.error(function (error) {
console.warn(error);
});
connection.start({ transport: ['longPolling'] });
return this;
};
connection.start();
})(jQuery);