Shahzad
Shahzad

Reputation: 1693

Add a method like extend to use fluently with ko.observable

I want to know if there is a way, I can write a new method test(), which can be used like ko.observable().test() and I can actually get a reference of the observable inside it. What I am trying to do is that instead of writing ko.observable().extend({extenderName: "value"}) I want to write ko.observable().extenderName(arg1, arg2)

Upvotes: 0

Views: 103

Answers (1)

dinesh ygv
dinesh ygv

Reputation: 1890

You can add it to ko.observable.fn as shown below:

ko.observable.fn.test = function(arg1) {
  console.log(this() + "," + arg1);
};

var obs = ko.observable("hello");

obs.test("world");
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

JS Fiddle: https://jsfiddle.net/c8z8c2r8/

Upvotes: 1

Related Questions