I am working on an existing project in Flex, and got tripped up for a day trying to figure out how data was getting from one list to another. I think I have figured it out but I’m hoping there are some Flex gurus out there that could shed some light on what is going on.
I’ll explain the situation:
There is a datasource lets call it ds, it loads an ArrayCollection (ac) into the model class (m). Then the view class (v) has two ListCollectionViews l1 and l2. Both l1 and l2 have filter functions so half the data get filtered out of l1 and the other half gets filtered out of l2.
Now this all makes sense so far, but if I add data to l1 or l2 it automagically gets added to m.ac. How is this happening? My best guess is that the underpinning data structure of all three lists is the same. But as I have spent so long stuck on this, I would love to know what is going on. Google has not been a lot of help on this one.
[More info]
As requested I’ll add some more details. Sorry I can’t copy and paste what I am working on.
inside model:
[Bindable] public var ac:ArrayCollection = null;
inside datasource:
private function resultHandlerac (event:ResultEvent) : void {
var ac:ArrayCollection = helperClass.arrayFromResultEvent (event);
m.ac = ac;
}
inside view:
[Bindable] public var l1:ListCollectionView;
[Bindable] public var l2:ListCollectionView;
private function resultHandlerac (ac:ArrayCollection) : void {
week1Hours = new ListCollectionView(ac);
week1Hours.filterFunction = function(i:Object) : Boolean { return i.WeekID
== 1 && !i.Removed; };
week2Hours = new ListCollectionView(hoursData);
week2Hours.filterFunction = function(i:Object) : Boolean { return i.WeekID
== 2 && !i.Removed; };
l1.refresh();
l2.refresh();
}
And there is a data grid that displays l1 and l2, every time a feild gets edited, inside the data grid, the three lists update. If more info is required please let me know. I can’t put the real code up here as this is not an open source project I am working on.
Your gut feeling is correct. ListCollectionView is just a wrapper around any IList that you give it as a source collection. It does not copy the items from the source collection.
This leaves the source collection intact when you filter on it. For example:
Yet at the same time you can still add items to it as if it were the wrapped Ilist itself:
Just have a look at the source code of ListCollectionView and you’ll see what happens: