Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created September 20, 2012 23:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamianEdwards/3758904 to your computer and use it in GitHub Desktop.
Save DamianEdwards/3758904 to your computer and use it in GitHub Desktop.
SignalR + Knockout Chat
<form data-bind="submit: sendMessage">
<input type="text" data-bind="value: newMessage" />
<input type="submit" value="Send" />
</form>
<ul data-bind="foreach: messages">
<li data-bind="text: $data"></li>
</ul>
<script src="Scripts/jquery-1.8.1.js"></script>
<script src="Scripts/jquery.signalR-0.5.3.js"></script>
<script src="signalr/hubs"></script>
<script src="Scripts/knockout-2.1.0.debug.js"></script>
<script>
var viewModel = $.signalR.chat;
$.extend(viewModel, {
newMessage: ko.observable(""),
messages: ko.observableArray(),
// Not sure we could make this nicer somehow, i.e. have the template bind directly to a server function
sendMessage: function () {
this.send(this.newMessage());
this.newMessage("");
},
// We could remove this function with more integration
messageAdded: function (msg) {
this.messages.push(msg);
}
});
$.signalR.hub.start().done(function () {
ko.applyBindings(viewModel);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment