Is there a way to ignore subscribers on a value change of the observable. Id like to change a value of an observable, but not execute it for the subscribers with knockout.js
-
As RP points out, this is not usually a good idea. What is your use case? Is there perhaps a *specific* subscriber you don't want to notify? – Kyeotic Aug 01 '13 at 06:02
-
i guess in that case you need to change your logic because knockout dose not meant to work like this. – ebram khalil Aug 01 '13 at 10:45
5 Answers
Normally this is not possible or advisable, as it potentially allows things to get out of sync in the dependency chains. Using the throttle extender is generally a good way to limit the amount of notifications that dependencies are receiving.
However, if you really want to do this, then one option would be to overwrite the notifySubscribers
function on an observable and have it check a flag.
Here is an extensions that adds this functionality to an observable:
ko.observable.fn.withPausing = function() {
this.notifySubscribers = function() {
if (!this.pauseNotifications) {
ko.subscribable.fn.notifySubscribers.apply(this, arguments);
}
};
this.sneakyUpdate = function(newValue) {
this.pauseNotifications = true;
this(newValue);
this.pauseNotifications = false;
};
return this;
};
You would add this to an observable like:
this.name = ko.observable("Bob").withPausing();
Then you would update it without notifications by doing:
this.name.sneakyUpdate("Ted");

- 114,592
- 18
- 291
- 211
-
2+1 for throttling. I wanted to avoid running multiple times a computed when setting multiple observables on which the computed depended. Result: Speed++ – billy Jan 17 '14 at 14:04
-
3In this case it is most suitable just to pause notifications so that you can update all your observables and having subscriptions triggered only at the very end (ryan's post http://www.knockmeout.net/2011/04/pausing-notifications-in-knockoutjs.html) – Flavia Obreja Feb 10 '14 at 15:02
-
2Nice answer. I think 'sneakyUpdate' could be better named 'poke', since KO provides a built-in 'peek' method, which does quite reads. 'poke' would be the writable version of 'peek'. – raider33 Oct 30 '15 at 14:13
-
There is one use case, I am pulling drop down options from ajax and setting drop down value 3 as selected by default and first one, and when user change option value I want to some logic , how to do that. – Pushker Yadav Jul 18 '17 at 12:16
-
Please, how can I do the same thing in TypeScript? With the code above it shows me "TS2339: Property 'withPausing' does not exist on type 'ObservableFunctions
'." – bondif Apr 13 '20 at 10:50
An even simpler approach:
ko.observable.fn.silentUpdate = function(value) {
this.notifySubscribers = function() {};
this(value);
this.notifySubscribers = function() {
ko.subscribable.fn.notifySubscribers.apply(this, arguments);
};
};
Use it as follows:
this.status = ko.observable("Happily Married");
this.walkIntoBar();
this.status.silentUpdate("Single");
this.walkOutOfBar(); // careful with exceptions
this.status.silentUpdate("Happily Married");
To be used with caution. We are dealing with an observable object so bad things can happen if you fail to notify your subscribers.

- 20,944
- 9
- 74
- 82
-
3
-
Please, how can I do the same thing in TypeScript? With the code above it shows me "TS2339: Property 'silentUpdate' does not exist on type 'ObservableFunctions
'." – bondif Apr 12 '20 at 22:58 -
Hi @bondif, im not very familiar with typescript sorry, it seems you are unable to override methods, which is the point of typescript really (ie strong typing checked at build time) but I'm sure there is some way to get around it. – Steven de Salas Apr 13 '20 at 09:37
I like the solution provided by @RP Niemeyer when all subscribers need to be ignored. However, for my case, I have an observable with 2-way bindings on a Select control. When using @RP Niemeyer, the Select control isn't updated. So, I really needed a way to turn off specific observers, not all. Here is a generalized solution for this case.
Add extensions methods for 'quiet' subscribe and 'quiet' writes.
ko.observable.fn.ignorePokeSubscribe = function (callback, thisValue, event){
var self = this;
this.subscribe(function(newValue) {
if (!self.paused)
callback(newValue);
}, thisValue, event);
return this;
};
ko.observable.fn.poke = function (newValue) {
this.paused = true;
var result = this(newValue);
this.paused = undefined;
return result;
};
You would subscribe to observable like:
this.name = ko.observable("Bob");
this.name.ignorePokeSubscribe(function(newValue) { /* handler */ }));
Then you would update it without specific notifications by doing:
this.name.poke("Ted"); // standard subscribers still get notified

- 5,685
- 2
- 32
- 41

- 1,633
- 1
- 19
- 21
-
2
-
1
-
Thanks, v useful to be able to suppress notifications for in code changes, just what I needed :) – TrystanC Jun 22 '16 at 10:19
-
One thing to watch out for, I suspect this won't work if the observable has a rate limit as the callback will fire after the pause is cleared. Also, would be nice if the subscribe returned the subscription so you can dispose it (like the standard subscribe method) – TrystanC Jun 23 '16 at 14:52
-
1RP Niemeyer's answer is good, but this one is my exact use-case: I want the UI to update still, but blocking a specific network channel because a whole series of changes were bulk communicated for efficiency. – Csaba Toth Mar 22 '17 at 02:11
-
Great solution - However, I think you want to return 'this.subscribe(...)' in ignorePokeSubscribe, as 'this' won't be similar to .subscribe() – Frame91 Jun 09 '17 at 23:05
-
Please, how can I do the same thing in TypeScript? With the code above it shows me "TS2339: Property 'ignorePokeSubscribe' does not exist on type 'ObservableFunctions
'." – bondif Apr 13 '20 at 10:47
I came to this question because I was building a paged datagrid. Only 10 rows were shown and each row has a checkbox. The table header had a (de)select all checkbox.
During load tests we discovered that clicking the select all checkbox resulted in updating 1000 observables at once. This took way too long.
It seemed that KO updated the html 1000 times even though only 10 observables were bound to HTML.
If anyone finds this question for the same reason, I would suggest looking into deferred updates. Deferred updates queue notifying subscribers, it notifies your subscribers after your 'thread' is done. Deferred updates is configurable per observable or for your whole application.

- 2,466
- 3
- 22
- 40
This may help to someone:
const candiesCount = ko.observable(0);
const loadCandiesPriceByAjax = () => { /*...*/ };
let isPriceLoadingEnabled = true;
candiesCount.subscribe(() => {
if (isPriceLoadingEnabled) {
loadCandiesPriceByAjax();
}
});
By changing isPriceLoadingEnabled
I can pause the subscription effect without really disabling it. For example:
const setCandiesCountWithoutAjaxingPrice = (count) => {
isPriceLoadingEnabled = false;
candiesCount(count);
isPriceLoadingEnabled = true;
};

- 1,267
- 15
- 19