Created
January 10, 2013 05:37
-
-
Save DamianEdwards/4499713 to your computer and use it in GitHub Desktop.
SignalR chat using Knockout sync models idea
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.3.js"></script> | |
<script src="Scripts/jquery.signalR-1.5.0.js"></script> | |
<script src="signalr/hubs"></script> | |
<script src="Scripts/knockout-2.1.0.js"></script> | |
<script> | |
function chatModel() { | |
this.newMessage = ko.observable(""); | |
this.messages = ko.observableArray(); | |
} | |
chatModel.prototype = { | |
sendMessage : function() { | |
this.messages.push(this.newMessage()); | |
this.newMessage(""); | |
} | |
}; | |
var conn = $.hubConnection(), | |
hub = conn.createProxy("chat"), | |
viewModel = $.signalR.ko.syncModel(chatModel, hub); | |
// Apply the bindings and start syncing | |
viewModel.applyBindings(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment