I have an issue where I have an underlying observable array, which is sorted and exposed via a computed observable. Although I now have an issue where if I throttle the computed observable it seems to cause an issue when calling removeAll
on the underlying array.
The scenario is quite convoluted but basically I have a set of about 0-200 rows in the buffer section which is the observable array, then I display sections of that array dependant upon which tab the user has selected. So out of 100 records I may only filter it down to 30 in the computed observable. There are points though where I need to invalidate the array and download a new list, which may come down individually or in batches so it is throttled to reduce un-needed re-evaluations. However when I try to invalidate the underlying array with removeAll
it seems to notify the view that stuff has changed and it tries to re-evaluate view level bindings, however some of them look at the underlying array which is now empty, but as the computed has not caught up it falls over.
So is there a way to force an evaluation on the computed after the removeAll on the array?
Here is an example of what I mean:
var currentFilterType = ko.observable(1); // this is changed in the UI by the user
var underlyingArray = ko.observableArray();
var filteredDetails = ko.computedObservable(filterPredicate);
function filterPredicate() {
// assume ko.linq is included, this is a simple version of whats happening
return underlyingArray.Where(function(x){ return x.FilterType() == currentFilterType; })
.OrderBy(function(x){ return x.DateCreated(); })
.ToObservableArray();
}
function invalidateData() {
underlyingArray.removeAll();
// fetch some more data to repopulate array
}
function doSomethingWithItem(data) {
// check something against the original array
}
// In the view usage would look like this
<!-- ko foreach: filteredDetails -->
<a data-bind="click: doSomethingWithItem"></a>
<!-- /ko -->