Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between subscribe and computed in knockout js

Tags:

knockout.js

Whats the main difference between using a computed function and a subscribe function in knockout.js?

like image 652
a_miguel6687 Avatar asked Sep 10 '13 00:09

a_miguel6687


People also ask

Which function used to perform computation in knockout JS?

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 Ko observable in knockout JS?

Knockout. js defines an important role when we want to detect and respond to changes on one object, we uses the 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.

What is applyBindings in Knockout?

applyBindings do, The first parameter says what view model object you want to use with the declarative bindings it activates. Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.

How do you assign a value to observable Knockout?

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

A computed observable is generally used to return a calculated value. Any observables (or other computeds) that are accessed as part of the computed's evaluation will become dependencies and trigger it to run again. A computed can be used like a subscription and is especially useful when you want to subscribe to multiple observables at once. However, you will not know which dependency triggered the change.

A manual subscription is specific to a single observable (or computed) and is passed the new value as its first argument. There is no concept of creating dependencies in the execution of the subscription's handler, as it will only ever be triggered when this observable changes.

like image 116
RP Niemeyer Avatar answered Oct 16 '22 08:10

RP Niemeyer