4

I'm trying to dispatch a custom event from a custom ItemRenderer

This is my custom event

package events
{
    import customClass.Product;

    import flash.events.Event;

    public class CopyProductEvent extends Event
    {
        public static const COPY_PRODUCT:String = "COPY_PRODUCT";
        public var picked:Prodotti;

        public function CopyProductEvent(type:String, picked:Product)
        {
            super(type);
            this.picked = picked;
        }
    }
}

In the itemRenderer I have a function that does that:

        private function sendEvent(o:Product):void
        {
            dispatchEvent(new CopyProductEvent(CopyProductEvent.COPY_PRODUCT,o));
        }

And in the main application I have a spark List and I tried to add an EventListener both to the application and the list itself, but they never be called...

    this.addEventListener(CopyProductEvent.COPY_PRODUCT,
        function(e:Product):void{
            ...
    });

    list.addEventListener(CopyProductEvent.COPY_PRODUCT,
        function(e:Product):void{
            ...
    });

Why?!? Where am I doing wrong?

The event from the function is dispatched correctly... I can't intercept it..

Marcx
  • 6,806
  • 5
  • 46
  • 69

1 Answers1

10

Sounds like your event isn't bubbling.

Add the bubbles argument (which by default, is false) in your Custom event constructor:

public function CopyProductEvent(type:String, picked:Product, bubbles:Boolean = true)
        {
            super(type,bubbles);
            this.picked = picked;
        }

A nice explanation on Event bubbling in AS3 can be found here: Event Bubbling in AS3

James
  • 969
  • 1
  • 8
  • 13
  • Thank you that worked... meanwhile I was waiting for your solution I found another one (less refined), from the itemrenderer when I dispacted the event I dispatched it from owner so: `owner.dispatchEvent...` and it worked too.., BTW your solution is way better... – Marcx Feb 15 '12 at 13:08
  • Yes, that solution will work too. The disadvantage with it however is that your itemRenderer is then tightly coupled with its owner so if you wanted to re-use the renderer you would have to rely on the same component structure being in place. Allowing the event to bubble allows for easy reuse. – James Feb 15 '12 at 14:49
  • yeah I got it... I did not know the bubble thing :) – Marcx Feb 15 '12 at 14:59
  • @Marcx I noticed that's my solution you're talking about. I _did_ mention bubbling in that answer, you know. Furthermore, I wouldn't know why that solution would tightly couple the ItemRenderer to its owner any more than it already is. You just dispatch an Event through the IEventDispatcher interface. You can reuse the itemrenderer in another list any time. You're not supposed to use an ItemRenderer outside of some kind of list component anyway. They are already coupled by design. – RIAstar Feb 15 '12 at 16:40
  • Also bubbling is expensive. If your ItemRenderer will dispatch a lot of events for some reason, it may be wiser not to choose the bubbling approach (I have to admit that's a corner case). Another example is the one in [that answer you were referring to](http://stackoverflow.com/questions/7193665): if you want to dispatch a generic "itemClick" event from the ItemRenderer, you don't want to dispatch that kind of event all over the place. You want it constrained to the parent list component. In the case described in the question, bubbling is the way to go though. – RIAstar Feb 15 '12 at 16:50