Reference
The following documentation describes how to construct and work with computed observables.
Constructing a computed observable¶
A computed observable can be constructed using one of the following forms:
-
fw.computed(evaluator [, targetObject, options])
This form supports the most common case of creating a computed observable.
-
evaluator
A function that is used to evaluate the computed observable's current value.
-
targetObject
If given, defines the value of
this
whenever Footwork invokes your callback functions. See the section on managingthis
for more information. -
options
An object with further properties for the computed observable. See the full list below.
-
-
fw.computed(options)
This single parameter form for creating a computed observable accepts a JavaScript object with any of the following properties.
-
read
Required. A function that is used to evaluate the computed observable's current value.
-
write
Optional. If given, makes the computed observable writable. This is a function that receives values that other code is trying to write to your computed observable. It's up to you to supply custom logic to handle the incoming values, typically by writing the values to some underlying observable(s).
-
owner
Optional. If given, defines the value of
this
whenever Footwork invokes yourread
orwrite
callbacks. -
pure
Optional. If this option is
true
, the computed observable will be set up as a pure computed observable. This option is an alternative to thefw.pureComputed
constructor. -
deferEvaluation
Optional. If this option is
true
, then the value of the computed observable will not be evaluated until something actually attempts to access its value or manually subscribes to it. By default, a computed observable has its value determined immediately during creation. -
disposeWhen
Optional. If given, this function is executed before each re-evaluation to determine if the computed observable should be disposed. A
true
-ish result will trigger disposal of the computed observable. -
disposeWhenNodeIsRemoved
Optional. If given, disposal of the computed observable will be triggered when the specified DOM node is removed by Footwork. This feature is used to dispose computed observables used in bindings when nodes are removed by the
template
and control-flow bindings.
-
-
fw.pureComputed(evaluator [, targetObject])
Constructs a pure computed observable using the given evaluator function and optional object to use for
this
. Unlikefw.computed
, this method doesn't accept anoptions
parameter. -
fw.pureComputed(options)
Constructs a pure computed observable using an
options
object. This accepts theread
,write
, andowner
options described above.
Using a computed observable¶
A computed observable provides the following functions:
-
dispose()
Manually disposes the computed observable, clearing all subscriptions to dependencies. This function is useful if you want to stop a computed observable from being updated or want to clean up memory for a computed observable that has dependencies on observables that won't be cleaned.
-
extend(extenders)
Applies the given extenders to the computed observable.
-
getDependenciesCount()
Returns the current number of dependencies of the computed observable.
-
getSubscriptionsCount([event])
Returns the current number of subscriptions (either from other computed observables or manual subscriptions) of the computed observable. Optionally, pass an event name (like
"change"
) to return just the count of subscriptions for that event. -
isActive()
Returns whether the computed observable may be updated in the future. A computed observable is inactive if it has no dependencies.
-
peek()
Returns the current value of the computed observable without creating a dependency (see the section on
peek
). -
subscribe(callback [, callbackTarget, event])
Registers a manual subscription to be notified of changes to the computed observable.
Determining the observable type¶
To check if a property is observable, computed, etc., use the following set of functions:
-
fw.isObservable()
Returns
true
for observables, observable arrays, and all computed observables. -
fw.isWritableObservable()
Returns
true
for observables, observable arrays, and writable computed observables (also aliased asfw.isWriteableObservable
). -
fw.isComputed()
Returns
true
for all computed observables. -
fw.isPureComputed()
Returns
true
for pure computed observables.
Using the computed context¶
During the execution of a computed observable's evaluator function, you can access fw.computedContext
to get information about the current computed property. It provides the following functions:
-
isInitial()
A function that returns
true
if called during the first ever evaluation of the current computed observable, orfalse
otherwise. For pure computed observables,isInitial()
is alwaysundefined
. -
getDependenciesCount()
Returns the number of dependencies of the computed observable detected so far during the current evaluation.
Note
fw.computedContext.getDependenciesCount()
is equivalent to calling getDependenciesCount()
on the computed observable itself. The reason that it also exists on fw.computedContext
is to provide a way of counting the dependencies during the first ever evaluation, before the computed observable has even finished being constructed.
Example:
var myComputed = fw.computed(function () { // ... Omitted: read some data that might be observable ... // Now let's inspect fw.computedContext var isFirstEvaluation = fw.computedContext.isInitial(), dependencyCount = fw.computedContext.getDependenciesCount(), console.log("Evaluating " + (isFirstEvaluation ? "for the first time" : "again")); console.log("By now, this computed has " + dependencyCount + " dependencies"); // ... Omitted: return the result ... });
These facilities are typically useful only in advanced scenarios, for example when your computed observable's primary purpose is to trigger some side-effect during its evaluator, and you want to perform some setup logic only during the first run, or only if it has at least one dependency (and hence might re-evaluate in the future). Most computed properties do not need to care whether they have been evaluated before, or how many dependencies they have.