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
About the Author
About the Reviewers

Custom functions

Knockout.js allows you to write your own custom functions to extend the capabilities of a standard Knockout.js object. The following objects from the Knockout.js hierarchy can be extended:

  • ko.subscribable (root object)

  • ko.computed (inherited from ko.subscribable)

  • ko.observable (inherited from ko.subscribable)

  • ko.observableArray (inherited from ko.observable)

If you write a custom function for an object, the inheritors of the object will also have the written function. For example, you can use a custom function of ko.observable with ko.observableArray.

An example with array filtering

Let's learn how to write a custom function with the following example. We will write the filterByProperty function for ko.observableArray. This function will filter the original array and keep only those items for which the specified property is equal to the specified value. The function body is as follows:

ko.observableArray.fn.filterByProperty = function(propName, matchValue) {
  return ko.computed...