ReactiveArray icon indicating copy to clipboard operation
ReactiveArray copied to clipboard

Use get() function for reactivity instead of list()

Open dalgard opened this issue 10 years ago • 1 comments

To get an item reactively from a ReactiveArray, if I understand correctly, I'm supposed to write myarr.list()[<index>]. However, I think using myarr.get(<index>) for reactivity would be much more in line with similar packages, such as ReactiveDict, ReactiveVar, and TemplateVar.

Please consider adding get() to the API.

dalgard avatar Aug 11 '15 09:08 dalgard

You made a good point in the thread https://github.com/ManuelDeLeon/viewmodel/issues/84#issuecomment-129982525 – it's not the individual items that are reactive, it's the whole array. However, I do think that my expectation in regard to the API has some significance.

More importantly, though:

Retrieving the array might happen reactively, as in the case of children() [manuel:viewmodel], but it might also have been simply initialized in a given context with var ra = new ReactiveArray and later passed on to other scopes.

When using some value on the array, the value would typically be referred to by its index. But some methods that transform the array (e.g. reverse() and splice()), unfortunately, are not functionally clean, in the sense that they update the array in place – and consequently, the item retrieved at a particular index may change.

And I don't see any practical way of limiting the reactive invalidation to the affected items.

Considering this – together with the point that the other reactive collection, ReactiveDict, has an intuitively similar API, albeit for single items only – I think a reactive get() method is the way to go.

dalgard avatar Aug 11 '15 22:08 dalgard