Blog

How to make observable property to notify always in KnockoutJS

Posted by Jaganathan Bantheswaran Blogs | JavaScript Sep 14, 2013
In this blog, let us see how to make an observable always notify the change to the subscribers.
In this blog, let us see how to make an observable always notify the change to the subscribers.

By default, the observable will notify the subscriber only if the value is changed otherwise it does not.

To enable the observable to notify always we have to use always extender.

For example, if you have a observable property called name, you want to display the change count for the name property.

 var vm = {
    name: ko.observable('Jagan').extend({notify: 'always'}),
    count: ko.observable(0)};

Here in the above code, you can see the extender notify used. if you set the notify to always, then the observable will notify the subscriber always even if the value changed is same as the original value.

 vm.name.subscribe(function() { this.count(this.count() + 1) }, vm);

We have a subscriber for the name observable above. whenever we get notification, will increase the count property to display the count of name changes.

<p> Name: <input data-bind="value: name" /> </p>

<p> Name has been changed <span data-bind="text: count"></span> times </p>

The above view bindings are used to demonstrate the example.

Output:

 


COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.