April 15, 2009 Update: Unfortunately, DynamicCollection performs badly when the underlying arrays exceed a thousand elements. Stay tuned for an update.
What if you have a few Arrays or ArrayCollections of the same object data, and you want them to display as a single dataProvider?
Using the below schema as an example, say you loaded a list of Projects from the DB and you want to display all the Companies in a single DataGrid. Or a list of all Employees. How would you do it?
Sure you could append them all to one big Array or ArrayCollection. Although you leave open the door for inconsistencies when you add a Company to some Project object.
This is where the DynamicCollection comes in.
Essentially it is an ArrayCollection containing Arrays of the same object type.
However, when iterated over, it behaves as a single ArrayCollection.
This means, when you alter the source arrays, the DynamicCollection will change also.
I have written the solution twice, once using Arrays and once using ArrayCollections as the elements. If you use the former, you have to call “refresh()” on the collection after updating any of the child arrays to enable databinding.
I’ve found it better to stick to Arrays noting that from Jon Rose’s blog and examples, you lose performance with a high number of ArrayCollections.
Below is the DynamicCollection class and an example implementation.
NOTE: I haven’t had the time to test it out with other ListBase implementations – feel free to comment on any issues you encounter.
(Right-click flash movie to view source).