Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  174] [ 7]  / answers: 1 / hits: 36417  / 11 Years ago, thu, august 1, 2013, 12:00:00

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


More From » events

 Answers
38

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);

[#76610] Wednesday, July 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cierra

Total Points: 504
Total Questions: 108
Total Answers: 109

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;