将事件从 ItemRenderer 调度到 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 版本,或者不适用于我的场景。 有人可以帮忙吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的事件应该扩展
Event
,而不是MenuEvent
。之后,事情就非常简单了。在调度程序中,您使用字符串(“UPDATE”),但在侦听器中,您使用等于“UPDATE_CATEGORY”的静态常量 UPDATE。
只需将以下内容更改
为:
First off, your event should extend
Event
, notMenuEvent
.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:
to this: