Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Dependency tracking


Binding handlers and computed observables need to re-evaluate when their observable dependencies update. This means keeping track of dependencies and subscribing to them. Three objects make up the dependency-tracking feature: observables, computed observables, and the dependency-detection module.

Here's the basic overview. When a computed is evaluated, it asks ko.dependencyDetection to start tracking things. When observables are accessed, they register themselves with ko.dependencyDetection. When the computed is done evaluating, it records all of the registered dependencies and subscribes to each of them.

Okay, now let's look at some code.

ko.dependencyDetection

The dependency detection module is very small—small enough to reproduce here in its entirety, actually:

ko.computedContext = ko.dependencyDetection = (function () {
  var outerFrames = [],
  currentFrame,
  lastId = 0;

  function getId() {
    return ++lastId;
  }

  function begin(options) {
    outerFrames.push...