Created
September 20, 2012 23:14
-
-
Save DamianEdwards/3758904 to your computer and use it in GitHub Desktop.
SignalR + Knockout Chat
This file contains hidden or 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
<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