views:

2181

answers:

3

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

+3  A: 

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.

cliff.meyers
+1 for mentioning weak refs, and I agree -- repeaters are a pain; I haven't yet found a compelling reason to choose them over their much more versatile counterparts.
Christian Nunciato
Does the recycleChildren property of the Repeater mitigate some of that performance hit?
Karthik
The problem is that a Repeater will create all of its children immediately whereas the List will create only what is visible on the screen (plus a few extra to act as a "buffer" when the user is scrolling). If you have a large collection it will be very expensive to instantiate all of that up front. Also from the docs on "recycleChildren": Setting it to true can increase performance, but is not appropriate in all situations. For example, if the previously created children have state information such as text typed in by a user, then this state will not get reset when the children are recycled.
cliff.meyers
A: 

Ok, seems I needed to give the vbox an id, after that I was able to access that id as an array of vboxes. After adding the event handler, I was able to use the event's currentTarget.getRepeaterItem() as a reference to the object in the ArrayCollection.

Cleaning up the event handlers was taken care of by looping over the vbox array and removing the handlers.

Not too worried about performance, as there are few items and they rarely change over the time the program is running, though those items would be different almost every run. Unless it's needlessly destroying and recreating items, it shouldn't be much of an issue.

mjard
Glad you figured it out. :) I think it would be worthwhile to learn the List/itemRenderer approach regardless. I was updating an application that would take ~10 seconds to render 40 items using a Repeater and reduced this down to ~100 ms using List + itemRenderer. It's the way to go. :)
cliff.meyers
Wow :( Ok, I'll be sure to look into that. Thanks.
mjard
One thing a repeater is good for is when you want to use a radio button in the component. Not that you can't do it with a list/renderer solution. It just works out of the box.
A: 

Hi , Can you explain me why some coders don't want to use "Repeater", rather they preferred using ItemRenderer in List . I'm using Repeater ever since and find its comfortable than using itemRenderer which I've found has a performance issue when I'd Profiling.

Glad to you hear your answer, guru's