signalr cleanup

This commit is contained in:
kay.one 2013-09-10 23:33:47 -07:00 committed by Keivan Beigi
parent feda4a9b67
commit 25e2c98c45
219 changed files with 2035 additions and 1495 deletions

View file

@ -5,59 +5,29 @@ define(
], function () {
_.extend(Backbone.Collection.prototype, {
bindSignalR: function (options) {
bindSignalR: function () {
if (!options) {
options = {};
}
var collection = this;
if (!options.url) {
console.assert(this.url, 'url must be provided or collection must have url');
options.url = this.url.replace('api', 'signalr');
}
var processMessage = function (options) {
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;
}
var model = new collection.model(options.resource, {parse: true});
collection.add(model, {merge: true});
console.log(options.action + ": %O", options.resource);
};
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();
});
this.signalRconnection.start({ transport:
require(
[
'longPolling'
] });
'app'
], function (app) {
collection.listenTo(app.vent, 'server:' + collection.url.replace('/api/', ''), processMessage)
});
return this;
},
unbindSignalR: function () {
if(this.signalRconnection){
this.signalRconnection.stop();
delete this.signalRconnection;
}
}});
});