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: