繁体   English   中英

从 ItemRenderer 到 Datagroup 的调度事件

[英]Dispatch event from ItemRenderer to Datagroup

事件调度在这里,我们又来了......

我正在尝试从 Datagroup 内的自定义 ItemRenderer 调度自定义事件,但没有成功。

// 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>

从我在调试器中看到的情况来看,事件是从 ItemRenderer 分派的,但它们永远不会被侦听器捕获(永远不会调用侦听器处理程序)。 许多建议在 stackoverflow 中飞来飞去,但大多数建议适用于较旧的 flex 版本,或者不适用于我的场景。 任何人都可以帮忙吗?

首先,您的事件应该扩展Event ,而不是MenuEvent

之后,这很简单。 在调度程序中,您使用的是字符串('UPDATE'),但在侦听器中,您使用的是 static 常量 UPDATE,它等于 'UPDATE_CATEGORY'。

只需更改此:

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

对此:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM