Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS - How do computed observables with conditional statements work

Tags:

knockout.js

KnockoutJS has the concept of computed observables, which are functions that depend on one or more observables. Knockout is able to determine the dependencies of a computed observable as described in the docs:

Whenever you declare a computed observable, KO immediately invokes its evaluator function to get its initial value. While your evaluator function is running, KO keeps a log of any observables (or computed observables) that your evaluator reads the value of.

Now, what I don't understand is, how this works if your computed observable contains conditional logic. If Knockout invokes the evaluator function, surely conditional logic might result in observables which the function depends on not being invoked?

I created this fiddle to test:

http://jsfiddle.net/bJK98/

var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);
    this.condition = ko.observable(false);

    // at the point of evaluation of this computed observabled, 'condition'
    // will be false, yet the dependecy to both firstName and lastName is
    // identified
    this.fullName = ko.computed(function() {
        return this.condition() ? this.firstName() : this.lastName();
    }, this);
};

However, somehow Knockout correctly identified the dependency to both firstName and lastName.

Can anyone explain how?

like image 667
ColinE Avatar asked Apr 13 '12 19:04

ColinE


People also ask

What computed observables?

Computed Observable is a function which is dependent on one or more Observables and automatically updates whenever its underlying Observables (dependencies) change. Computed Observables can be chained.

What is pure computed in knockout JS?

A pure computed observable automatically switches between two states based on whether it has change subscribers. Whenever it has no change subscribers, it is sleeping. When entering the sleeping state, it disposes all subscriptions to its dependencies.

What does Ko observable do?

An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted. The main advantage of KO is that it updates our UI automatically when the view model changes.

How do I assign a value to Knockout observable?

To create an observable, assign the ko. observable function to the variable. A default value can be specified in the constructor of the call. Knockout then converts your variable into a function and tracks when the value changes, in order to notify the UI elements associated with the variable.


1 Answers

dependencies are tracked again each time that a dependentObservable is re-evaluated. So, if you have conditional logic, then the branch that is not hit will not contribute to the dependencies.

In your fiddle, if you edit the firstName, then the value is not updated until you toggle condition. At that point, lastName is no longer a dependency, so changes to it do not trigger the dependentObservable.

It is not really more complex than the original description. The basic thing to remember is that dependencies are recorded each time that it is re-evaluated.

like image 148
RP Niemeyer Avatar answered Oct 15 '22 01:10

RP Niemeyer