andrey.shedko
andrey.shedko

Reputation: 3238

Knockout.js: Subscribe or computed observable?

Let me explain. I have webapi controller that return some values later bind to UI with Knockout.js. One of the values I'm getting from the other controller but need to bind to the same element. In order to update ViewModel with new value, should I use subscribe method or computed? I'm new to Knockout.js, so please do not do not judge me strictly - many things making me confused even after reading docs.

Upvotes: 2

Views: 460

Answers (1)

user2864740
user2864740

Reputation: 62015

In general:

  1. Use observables for creating any view-model properties, computed or otherwise.

    Code can also share the same observable object between multiple view-models, such that both literally provide the same observable. This should be done only after consideration, but it eliminates a "proxy" observable entirely.

    In addition, it's also possible to use aggregation to allow a view to bind onto multiple-view models (either as siblings or through one vm being accessible as a property/observable of the other).

  2. Use subscriptions for events and processing outside view-model properties.

    In a few cases subscriptions can be useful for view-model maintenance, but they should generally not be preferred over standard view-model observables.

Upvotes: 1

Related Questions