Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.