Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout: computed observable vs function

When using knockout, what is the advantage of using read-only computed observables rather than simple functions?

Take the following viewmodel constructor and html snippet, for example: ​

var ViewModel = function(){     var self = this;     self.someProperty = ko.observable("abc");     self.anotherProperty = ko.observable("xyz");     self.someComputedProperty = function(){         return self.someProperty() + self.anotherProperty();     };     };  <input data-bind="value: someProperty"/> <input data-bind="value: anotherProperty"/> <p data-bind="text: someComputedProperty()"></p> 

Everything here seems to work as you'd expect, so is there a reason why I should instead use:

​var ViewModel = function(){     var self = this;     self.someProperty = ko.observable("abc");     self.anotherProperty = ko.observable("xyz");     self.someComputedProperty = ko.computed(function(){         return self.someProperty() + self.anotherProperty();     });     };   <input data-bind="value: someProperty"/> <input data-bind="value: anotherProperty"/> <p data-bind="text: someComputedProperty"></p> 

I notice that the documentation at http://knockoutjs.com/documentation/computedObservables.html states that "...declarative bindings are simply implemented as computed observables", so does that mean there's need for me to use them explicitly in my viewmodels?

like image 402
Duncan Avatar asked Jul 17 '12 17:07

Duncan


People also ask

Which function is used for computation in knockout?

In some scenarios, it is useful to programmatically determine if you are dealing with a computed observable. Knockout provides a utility function, ko. isComputed to help with this situation. For example, you might want to exclude computed observables from data that you are sending back to the server.

Is knockout observable?

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.

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.


1 Answers

If the only purpose of your computed observable is to do a simple binding against it, then using a function would be equivalent. Bindings are implemented inside of a computed observable to track the dependencies, so it will re-trigger your binding when any of the observables change.

Here are a few things to consider about computed observables vs. a function

  • the value of a computed observable is cached, so it is only updated when it is created and when a dependency is updated. For a regular function, you would need to execute the logic each time. If many things depend on that value (say each item in a collection is binding against a value from the parent), then this logic will be getting run again and again.

  • in your JavaScript, you are also free to use computed observables like you would other observables. This means that you can create manual subscriptions against them and depend on them from other computeds (calling a function would also create this dependency). You can rely on the normal utility methods in KO like ko.utils.unwrapObservable to generically determine if you need to call it as a function or not to retrieve the value.

  • if ultimately you want to ship the value to the server, a computed observable will naturally appear in your JSON output, while a value that is the result of a normal function will just disappear when converted to JSON (you would have to do more work to populate a property from that function first).

like image 139
RP Niemeyer Avatar answered Sep 24 '22 17:09

RP Niemeyer