I have a custom Flex Container component written in AS3, called StatisticsContainer. When used in the application, it contains various custom Label components called StatisticsBoxes. So there might be a StatisticsContainer containing 3 StatisticsBoxes: "averageAge", "divorceRate" and "infantMortalityRate".
The StatisticsContainer needs to be able to reference and operate on all the StatisticsBoxes. However I don't want to hard-code the references into StatisticsContainer, as there will be various different instances of StatisticsContainer with different StatisticsBoxes in them.
So how do I dynamically give StatisticsContainer an ArrayCollection of all the StatisticsBoxes it contains?
So far I started with a function like this in the creationComplete of StatisticsContainer:
for (var i:int = 0; i < numElements; i++) {
if (getElementAt(i) is StatisticsBox) {
statisticsBoxes.addItem(getElementAt(i));
}
}
This works only if the StatisticsBoxes are direct subchildren. Anyway it feels a bit hacky.
Then I tried listening for a CreationComplete event in StatisticsContainer. However these don't arrive from StatisticsBox because they don't bubble.
In the end I created my own event which bubbles and I fire it on creationComplete in StatisticsBox, and listen for it in StatisticsContainer. This works, but is this really the best way to do it?