Daisy-chained ListCollectionView not reflecting changes to filter in source collection

855 Views Asked by At

ListCollectionView is often promoted as a great way to daisy-chain different views of the same data. You have some source collection and then use ListCollectionView + filterFunction to wrap the source data and present a subset of that source collection. When the source collection changes (either adds or removals), the chained LCV gets updated automatically. However, if the filterFunction for the source collection changes, it does not appear to propagate to the chained LCV:

ArrayCollection source = new ArrayCollection(new Array({name: 'Matt'}, {name: 'John'}, {name: 'Zach'}));
ListCollectionView justMatts = new ListCollectionView(source);
justMatts.filterFunction(function(obj: Object):Boolean { obj.name == 'Matt' });
justMatts.refresh();

If I add a new Matt object to source, justMatts will pick up that change and rerun its filter as expected.

However, if I add a filter to source to exclude Matt objects, justMatts doesn't pick up that change to the source's filter:

source.filterFunction(function(obj: Object):Boolean { obj.name != 'Matt' });
source.refresh();

At this point, I would expect justMatts to receive a CollectionChangeEvent and rerun its filter. Since all Matts have been filtered out of the source object, justMatts would be empty as well. However if you look in ListCollectionView code, they don't handle a CollectionEventKind.REFRESH which is what gets fired when source gets a new filter applied. Am I doing something wrong here (using wrong class, etc) or does nothing in Flex support the changes to source filter.

I can get this working as I expect it to by manually dispatching a CollectionEventKind.RESET on source, but I was hoping to not have to subclass anything.

1

There are 1 best solutions below

2
On

I wouldn't expect it to work like that at all. I would expect that the source of both the ArrayCollection (which is a LisCollectionView) and the explict ListCollectionView to be identical, with their filters operating independently.

However, if you poke around in the source code for these two Classes, you may well find that either filtering the AC doesn't dispatch a CollectionChange event (doubtful, since it requires refresh), or LCV doesn't listen for the event and update.

If you apply the filter to the list first and THEN make the ListCollectionView, do you see the results you expect? If not, then I think you've misread how this is supposed to work. If you do, then I have ;-)