Reputation: 27
What is difference between KnockoutObservable<string[]> and KnockoutObservable<string>[]
When to use either of them?
Upvotes: 0
Views: 949
Reputation: 4470
If you want an Observable Array, you should be using https://knockoutjs.com/documentation/observableArrays.html
Unless the typescript facade is doing something unexpected (I'm unfamiliar with it), chances are both an array of Knockout Observables, or a Knockout Observable of type array are going to be incorrect.
But to answer your question, they are going to have very different performance overheads, and different uses.
A plain KnockoutObservable of type array, is only going to update when the entire array is replaced. you will not receive notifications when the array is mutated, I can't think of any reason off the top of my head for using it, except for maybe some really data heavy operations, where you are receiving a stream of array information.
However, an array of KnockOut Observables, will give a really heavy performance overhead.
You are creating a KnockoutObservable for each element . Each of those elements are an individual Knockout Observable that can be listened to independently of each other. Just rather then having a property name to bind to, you have an array and key/index.
In 99% of cases, you are probably looking for an ObservableArray which is different.
Upvotes: 0
Reputation: 1847
When you subscribe to KnockoutObservable<string[]>
, then you will receive a string array (string[]
)
The other one is an array of observables (KnockoutObservable<string>
), each one resolving to a result with a type string.
When you wish to receive the string array, you should use KnockoutObservable<string[]>
Upvotes: 2