I have a List that uses a custom ItemRenderer. Is there a way for the owner (List) to dispatch a custom event I have created, to all instances of it’s ItemRenderer?
For example, I want to dispatch an event that will add text to a textbox within the item renderer. One or more item renderers will be able to respond to this event depending on certain user interactions.
Is there a way?
Thanks 🙂
If your custom ItemRenderer instances have access to the List instance somehow, it’s straigh forward – just register your event handling method:
If you don’t have direct access (which I assume), you can still do it indirectly, e.g. through a delegate of the ItemRenderer’s class object. Make an instance of a subclass of EventDispatcher a static property of the ItemRenderer class, and in your ItemRenderer constructor, register an event handler with it:
Now, when you create theList (either in ActionScript or in the initialize event handler of your MXML component), make a new YourCustomEventDispatcher, give to it a reference to theList, and add it to ItemRenderer. YourCustomEventDispatcher registers a private event handler for YourCustomEvent with theList, and just redispatches it. Since all ItemRenderer instances have in turn registered for YourCustomEvent with YourCustomEventDispatcher, theLists’s YourCustomEvent reaches all ItemRenderers via one hop.
This is basically an implementation of the Observer design pattern.