Skip to content

Instantly share code, notes, and snippets.

@rahulpnath
Created July 9, 2014 06:45
Show Gist options
  • Save rahulpnath/8362e3a4ebe0181ca362 to your computer and use it in GitHub Desktop.
Save rahulpnath/8362e3a4ebe0181ca362 to your computer and use it in GitHub Desktop.
A Pen by Rahul P Nath.
<div>
<button onClick="updateName()" >Update Name</button></div>
<div>
<label>Name:</label>
<label data-bind="text: Name"></label>
</div>
// View Model
// Having the properties as observables is similar to having INotifyPropertyChanged implemented
// and raising the propertychanged event for the property in the setter method.
var personVM = {
Name: ko.observable("Rahul"),
Age: ko.observable(50)
};
// Bind the View Model to the document
// This is similar to binding to DataContext in XAML
ko.applyBindings(personVM);
// This is a function that will toggle the name, which will trigger a UI refresh
function updateName(){
if(personVM.Name() === "Rahul"){
personVM.Name("Rahul Nath");
}
else {
personVM.Name("Rahul");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment