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 fromko.subscribable
)ko.observable
(inherited fromko.subscribable
)ko.observableArray
(inherited fromko.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
.
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...