Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created September 9, 2011 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfowl/1206882 to your computer and use it in GitHub Desktop.
Save davidfowl/1206882 to your computer and use it in GitHub Desktop.
SignalR knockout
/// <reference path="jquery-1.6.2.js" />
(function (window) {
"use strict";
if (typeof (window.signalR) !== "function") {
throw "SignalR: SignalR is not loaded. Please ensure SignalR.js is referenced before ~/signalr/hubs.";
}
var viewModels = {};
var oldProcessState = window.signalR.hub.processState;
// TODO: Make this per hub
window.signalR.hub.processState = function (hubName, newState, state) {
oldProcessState(hubName, newState, state);
$.each(newState, function (key) {
var hubViewModels = viewModels[hubName];
if (hubViewModels) {
$.each(hubViewModels, function () {
if (this[key]) {
this[key](newState[key]);
}
});
}
});
};
$.each(window.signalR, function () {
var that = this;
if (that._) {
that._.ignoreMembers.push('ko');
that.ko = function (viewModel) {
var hubViewModels = viewModels[that._.hubName];
if (!hubViewModels) {
hubViewModels = [];
viewModels[that._.hubName] = hubViewModels;
}
hubViewModels.push(viewModel);
};
}
});
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment