This is an example for the blog post 'KnockoutJS for XAML Developers' - http://www.rahulpnath.com/blog/knockoutjs-for-xaml-developers/
A Pen by Rahul P Nath on CodePen.
<div> | |
<button onClick="updateName()" >Update Name</button></div> | |
<div> | |
<label>Name:</label> | |
<label data-bind="text: Name"></label> | |
</div> |
This is an example for the blog post 'KnockoutJS for XAML Developers' - http://www.rahulpnath.com/blog/knockoutjs-for-xaml-developers/
A Pen by Rahul P Nath on CodePen.
// 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"); | |
} | |
} |