Book Image

Getting Started with Knockout.js for .NET Developers

By : Andrey Ankshin
Book Image

Getting Started with Knockout.js for .NET Developers

By: Andrey Ankshin

Overview of this book

Table of Contents (14 chapters)
Getting Started with Knockout.js for .NET Developers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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);
  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">&lt...