Created
September 9, 2011 17:57
-
-
Save davidfowl/1206882 to your computer and use it in GitHub Desktop.
SignalR knockout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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