In this section, we will discuss a special way to create complex properties that depend on our existing observable properties. The standard approach in the Knockout.js library is computed observables. We will consider these kinds of properties with a simple example.
Let's add two base observable properties in the Model (the person
object) and ViewModel (the personViewModel
object): firstName
and lastName
(PersonalPage-Computed1.html
). The initial values of these properties will be John
and Doe
, respectively.
The Model will be as follows:
var person = { firstName: "John", lastName: "Doe" };
The ViewModel will be as follows:
var personViewModel = function() { var self = this; self.firstName = ko.observable(person.firstName); self.lastName = ko.observable(person.lastName); };
The View will be as follows:
The first name: <span data-bind="text: firstName"></span><br /> The last name: <span data-bind="text: lastName"><...