In a function that takes a generator, that may be a plain JS Array or an ko.observableArray
, how can I use the observableArray
with minimal overhead?
This is what the code currently looks like:
function (itemGenerator) { // returns observableArray or JS array
var allItems = ko.observable();
// triggered after user interaction
var itemsFromGenerator = itemGenerator();
if (ko.isObservable(itemsFromGenerator)) {
allItems(itemsFromGenerator());
itemsFromGenerator.subscribe(newValue => {
allItems(newValue)
});
} else {
allItems(children);
}
}
Is there any way to replace allItems
with the observableArray
if itemsFromGenerator
is an observableArray
? So that I don't have to subscribe to further changes "manually" and have to copies of the data around.
(This is used in a TreeView implementation, where I only want to generate the items array when a node is expanded.)