事件在这里发送我们再次......
我正在尝试从数据组内的自定义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版本,或者不适用于我的场景。 有人可以帮忙吗?
答案 0 :(得分:4)
首先,您的活动应该延伸Event
,而不是MenuEvent
。
之后,它非常简单。在调度程序中,您使用的是字符串('UPDATE'),但在您的监听器中,您使用的静态常量UPDATE等于'UPDATE_CATEGORY'。
只需改变一下:
var categoryEvent:CategoryEvent = new CategoryEvent("UPDATE", data.id);
到此:
var categoryEvent:CategoryEvent = new CategoryEvent(CategoryEvent.UPDATE, data.id);