R – Flex Repeaters and EventListeners

actionscript-3apache-flex

At first glance, the Repeater control looks extremely powerful, but now I'm having second thoughts.

Problem at hand:
Adding Event Listeners to the children

Repeater object is bound to and iterates through an ArrayCollection creating new Vbox children for each item. Now for each child, the Repeater object will fire off a "repeat" event, where I'm tempted to add the eventlistener (for mouse events). No problems so far, but, what happens when the ArrayCollection changes, how should I remove the EventListeners for all the old children? Is there an array of children containing my Vbox instances that I'm skipping over in the docs? Are eventlisteners cleaned up nicely when the object they are attached to is destroyed?

-C coder lost in flex/actionscript

Best Answer

I would avoid using Repeaters entirely. From a performance standpoint they are very slow because the items are frequently destroyed and recreated. You are better off using a List-based control and implementing an itemRenderer.

Event listeners by default are strong references, so if you do not remove your event listeners it will prevent the object from being garbage collected. You can use the optional 5th parameter in the addEventListener called "weakReference" and set the value to true to add an event listener that will not prevent GC.

To better deal with events in your itemRender component, you can implement the IDropInListItemRenderer interface. This will give you access to "listData" which has an "owner" property which is the actual List object itself. In your itemRenderer, dispatch a custom Event containing the necessary data through the owner. If you add an event listener to the List control after it's created then you can do the event handling in the component containing the control.