Skip to content

Instantly share code, notes, and snippets.

@drawveloper
Created February 21, 2016 12:57
Show Gist options
  • Save drawveloper/da741e7f8b6929ba098f to your computer and use it in GitHub Desktop.
Save drawveloper/da741e7f8b6929ba098f to your computer and use it in GitHub Desktop.
// View (a template)
<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>
// ViewModel (diplay data… and logic?)
var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
// Knockout tracks dependencies automatically.
// It knows that fullName depends on firstName and lastName,
// because these get called when evaluating fullName.
this.fullName = ko.pureComputed(function() {
return this.firstName() + “ “ + this.lastName();
}, this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment