omni
omni

Reputation: 4485

Dispatch event from ItemRenderer to Datagroup

Eventdispatching here we are again...

I'm trying to dispatch a custom event from an custom ItemRenderer inside a Datagroup with no success.

// CUSTOM EVENT
import flash.events.Event;

public class CategoryEvent extends mx.events.MenuEvent
{
    public static const UPDATE:String   = "UPDATE_CATEGORY";
    public var categoryId:Number;

    public function CategoryEvent(type:String, categoryId:Number)   
    {
        this.categoryId = categoryId;
        super(type, true, false);
    }

    override public function clone():Event
    {
        return new CategoryEvent(type, categoryId);
    }
}

 // Main Class

<s:Application  xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                creationComplete="init()">

    <fx:Script>
            <![CDATA[
            // All my imports here 

            private function init():void
            {   
                this.addEventListener(CategoryEvent.UPDATE, updateCategories);
                this.tree.addEventListener(CategoryEvent.UPDATE, updateCategories);
            }

            private function updateCategories(event:CategoryEvent):void
            {
                //IS NEVER CALLED - DONT KNOW Y
            }
            ]]>
    </fx:Script>

    <s:DataGroup id="tree" dataProvider="{this.getCategorysChildrenResult.lastResult}" itemRenderer="components.Category"></s:DataGroup>

// Custom Item Renderer
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" >
    <fx:Script>
        <![CDATA[
            import ...events.CategoryEvent;
            import ...valueObjects.Category;
            import flash.events.EventDispatcher;


            protected function update(id:Number):void
            {

                var categoryEvent:CategoryEvent = new CategoryEvent("UPDATE", data.id);
                // tried all variations...
                dispatchEvent(categoryEvent);
                owner.dispatchEvent(categoryEvent);
                parent.dispatchEvent(categoryEvent);
                parentApplication.dispatchEvent(categoryEvent);

            }


        ]]>
    </fx:Script>

    <s:Group>
        <s:BorderContainer>
            <mx:Text buttonMode="true" text="{data.name}"  />
            <s:BorderContainer click="{this.update(data.id)}" buttonMode="true" />
        </s:BorderContainer>
    </s:Group>
</s:ItemRenderer>

From what i saw in the debugger, the events are dispatched from the ItemRenderer, but they never get catched by the listener (listener handler is never called). Many suggestions fly around about that in stackoverflow, but most for older flex versions or not practicle for my scenario. Can anyone help?

Upvotes: 1

Views: 4924

Answers (1)

J_A_X
J_A_X

Reputation: 12847

First off, your event should extend Event, not MenuEvent.

After that, it's pretty simple. In the dispatcher you're using a string ('UPDATE') but in your listener you're using the static constant UPDATE which equals 'UPDATE_CATEGORY'.

Just change this:

var categoryEvent:CategoryEvent = new CategoryEvent("UPDATE", data.id);

to this:

var categoryEvent:CategoryEvent = new CategoryEvent(CategoryEvent.UPDATE, data.id);

Upvotes: 4

Related Questions