Created
June 9, 2024 17:53
-
-
Save ChrisMoney/217bb988ed03bde8a50793214cd1e358 to your computer and use it in GitHub Desktop.
Knockout Javascript example
This file contains 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
<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