Is there a good way to roughly achieve the following concept:
var computed = ko.computed(function() {
readSomeObservables(); //<-- if these change, notify computed
ko.stopCollectingDependencies();
readSomeMoreObservables(); //<-- if these change, do not notify computed
ko.resumeCollectingDependencies();
});
I am aware of peek()
, but in this case the computed is invoking methods that were provided from an external module, and the design calls for it to be purely incidental if those methods happen to involve observables.
I have one solution, which is to roughly do this:
window.setTimeout(function() {
readSomeMoreObservables();
}, 0);
But for obvious reasons, that is hardly ideal and leads to undesired behavior in some situations.
For later visitors ...
Version 3.3 has made public ko.ignoreDependencies(callback, callbackTarget, callbackArgs). This is the method used internally by the binding handler processing to avoid creating dependancies from the init function call.
See http://www.knockmeout.net/2015/02/knockout-3-3-released.html
What about a combination. Create a temp computed for the subscribeables you need to read but do not want to subscribe to. Changing them would update the temp computed but that could be a cheap operation. Your real computed reads the tempComputed with peek accessing the currently cached value.
// this one is updated
// if any of the subscribeables used in readSomeMoreObservables changes
// but that is hopefully cheap
var tempComputed = ko.computed(function() {
readSomeMoreObservables();
});
var computed = ko.computed(function() {
readSomeObservables(); //<-- if these change, notify computed
// do not update on readSomeMoreObservables
tempComputed.peek();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With