Skip to content

Commit

Permalink
Document "spectate" event (see #2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Jan 2, 2019
1 parent d0b8f41 commit d3d13fc
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions documentation/computed-pure.md
Expand Up @@ -140,18 +140,22 @@ In some scenarios, it is useful to programmatically determine if you are dealing

### State-change notifications

A pure computed observable notifies an `awake` event (using its current value) whenever it enters the *listening* state and notifies an `asleep` event (using an `undefined` value) whevener it enter the *sleeping* state. You won't normally need to know about the internal state of your computed observables. But since the internal state can correspond to whether the computed observable is bound to the view or not, you might use that information to do some view-model initialization or cleanup.
A pure computed observable notifies some events that allow you to respond to changes to the state of the observable.

this.someComputedThatWillBeBound = ko.pureComputed(function () {
...
}, this);
- `awake` — Whenever the computed observable enters the *listening* state, it notifies an `awake` event using its current value. (The `awake` event also applies to normal computed observables created with the `deferEvaluation` option.) You won't normally need to know about the internal state of your observables. But since the internal state can correspond to whether the observable is bound to the view or not, you might use that information to do some view-model initialization or cleanup.

this.someComputedThatWillBeBound = ko.pureComputed(function () {
...
}, this);

this.someComputedThatWillBeBound.subscribe(function () {
// do something when this is bound
}, this, "awake");

this.someComputedThatWillBeBound.subscribe(function () {
// do something when this is bound
}, this, "awake");
- `asleep` — Whevener the computed observable enters the *sleeping* state, it notifies an `asleep` event with a value of `undefined`.

this.someComputedThatWillBeBound.subscribe(function () {
// do something when this is un-bound
}, this, "asleep");
this.someComputedThatWillBeBound.subscribe(function () {
// do something when this is un-bound
}, this, "asleep");

(The `awake` event also applies to normal computed observables created with the `deferEvaluation` option.)
- `spectate` — Whenever the computed observable **records** a change to its value, even while sleeping, it notifies a `spectate` event with the new value. (The `spectate` event applies to any type of observable but is generally most useful for pure computed observables.) This event allows you to track the current value of the observable without affecting its sleeping/waking state. Also note that when using rate-limiting or deferred updates, the "spectated" values might include intermediate values that aren't captured by `change` notifications.

0 comments on commit d3d13fc

Please sign in to comment.