Computed observables
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.
Using a computed observable
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); ...
Get Getting Started with Knockout.js for .NET Developers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.