将事件从 ItemRenderer 调度到 Datagroup

发布于 2024-11-03 20:51:12 字数 2819 浏览 2 评论 0原文

事件调度又来了...

我试图从数据组内的自定义 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 版本,或者不适用于我的场景。 有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2024-11-10 20:51:12

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

之后,事情就非常简单了。在调度程序中,您使用字符串(“UPDATE”),但在侦听器中,您使用等于“UPDATE_CATEGORY”的静态常量 UPDATE。

只需将以下内容更改

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

为:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文