Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM Computer
23-Apr-2025Understanding 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()