Dependencies and tracking in computed observables
Ask by ICSM Computer
Updated 23 Apr 2025
Understanding dependencies and tracking in
ko.computedobservables is key to unlocking Knockout's full reactive power.What Is a
computedObservable?A
ko.computedobservable is a derived value that automatically updates whenever its dependencies (observables) change.Think of it like a spreadsheet formula — change the input cells, and the result cell updates automatically.
How Dependency Tracking Works
Knockout automatically tracks any observables you reference inside a
computedfunction. When those observables change, the computed value re-evaluates itself.Basic Example:
firstNameorlastNamechanges,fullNameauto-updates.Key Rule:
Dependency Example
Notice:
"Computed running"only logs when one of its dependencies is accessed/changed.Manual Re-evaluation? Not Needed!
You don't call
sum()to recalculate. Just call it to get the current value — Knockout handles updates automatically.Ignored Observables?
If you reference an observable outside the
computed, it won’t be tracked.Advanced:
pureComputedUse
ko.pureComputed()if you want a computed that’s:Summary
ko.computed()ko.pureComputed()