Does a Knockout computed observable recover when a dependency observable is replaced after applyBindings?
0 reputation · 11 Jan 2024, 21:45 UTC
I have a view model where several computed observables derive values from plain observables, and everything is bound with ko.applyBindings at page load. In some flows the model's shape effectively changes at runtime: an observable that a computed depends on may be replaced with a new observable instance, or a property may be added to the view model after binding has already happened.
My understanding is that a computed re-evaluates only when the dependencies it captured during its last evaluation change. If the underlying observable instance is swapped out, the computed may still be subscribed to the old instance and never see updates from the new one, leaving the UI stale. I am unsure whether this is guaranteed behavior or depends on how the replacement is done.
Assume Knockout 3.5.x. I want a pattern where the computed always reflects the current dependency, without tearing down and re-applying all bindings.
Does replacing an observable instance after applyBindings reliably leave dependent computeds subscribed to the stale instance, or are there documented cases where the binding refreshes on its own?
Is wrapping the dependency in another observable (so the inner instance can be swapped) the intended way to keep computeds live, and does that interact correctly with dispose and binding-context cleanup in 3.x?