Given a computed property
vm.checkedValueCount = ko.computed(function(){
var observables = getCurrentValues(); //an array of ko.observable[]
return _.filter(observables, function(v) { return v() }).length;
});
suppose getCurrentValues() can return different sets of observables which are modified elsewhere in the code (and comes from a more complex structure than an observableArray).
I need checkedValueCount
to update whenever
- one of its dependencies change
- getCurrentValues() returns a different set of observables.
The problem is that ko.computed
seems to memoize the last returned value and only update when a dependency updates. This handles the first case but not the latter.
What I'm looking for is a way to force checkedValueCount to re-run. Something which I can use like:
changeCurrentValues();
vm.checkeValueCount.recalculate();
Put most simply, given that I have
a = ko.computed(function() { return Math.random() })
how can I force invoking a()
twice to return different values.
since there is no straight forward way to force update a computed, i have created an observable named toForceComputedUpdate, and i called it within the computed function so the computed will listen to its update, then to force update i call it like this toForceComputedUpdate(Math.random)
There is a method to force recalculation of all observables depending on yours:
This answer is conceptually the same as the one @josh gave, but presented as a more generic wrapper. Note: this version is for a 'writeable' computed.
I'm using Typescript so I've included the ts.d definition first. So ignore this first part if not relevant to you.
Notifying-writeable-computed
A wrapper for a writable
observable
that always causes subscribers to be notified - even if no observables were updated as a result of thewrite
callJust replace
function<T> (options: KnockoutComputedDefine<T>, context)
withfunction(options, context)
if you don't use Typescript.The main use case for this is when you are updating something that would not otherwise trigger a change in an observable that is 'visited' by the
read
function.For instance I am using LocalStorage to set some values, but there is no change to any observable to trigger re-evaluation.
Note that all I needed to change was
ko.computed
toko.notifyingWritableComputed
and then everything takes care of itself.When I call
hasUserClickedFooButton(true)
then the 'dummy' observable is incremented forcing any subscribers (and their subscribers) to get the new value when the value in LocalStorage is updated.(Note: you may think the
notify: 'always'
extender is an option here - but that's something different).There is an additional solution for a computed observable that is only readble:
From @mbest comment https://github.com/knockout/knockout/issues/1019.
I assume getCurrentValues() is a function. If you could make it a computed, your checkedValueCount would just magically start working.
Can you make getCurrentValues be a computed instead of a function?
I realized my first answer missed your point, and won't solve your issue.
The problem is that a computed will only reevaluate if there is some observable that forces it to re-evaluate. There is no native way to force a computed to re-evaluate.
However, you can get around this with some hackery by creating a dummy observable value and then telling its subscribers that it has changed.
Here is a Fiddle showing this approach