Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created June 9, 2024 17:53
Show Gist options
  • Save ChrisMoney/217bb988ed03bde8a50793214cd1e358 to your computer and use it in GitHub Desktop.
Save ChrisMoney/217bb988ed03bde8a50793214cd1e358 to your computer and use it in GitHub Desktop.
Knockout Javascript example
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>
// data model
var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.pureComputed(function() {
// Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment