Extending observables

Observables are a very useful and powerful part of Knockout.js. They allow you to create flexible and reliable applications. However, sometimes, you need to extend the opportunities of your observable properties. The extenders help you! As usual, let's proceed with some examples from the official documentation in detail.

Creating an extender

In this section, we will write a simple extender, as follows:

ko.extenders.logChange = function(target, option) { // 1
  target.subscribe(function(newValue) { // 2
    console.log(option + ": " + newValue); // 3
  });
  return target; // 4
};

Let's discuss it in detail:

  1. We use the ko.extenders object to create a new extender by adding a new property. In the preceding example, the property name is

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.