2011-04-27 46 views
1

这里Eventdispatching我们再次...调度事件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版本,或者不适合我的场景。 任何人都可以帮忙吗?

回答

4

首先,您的活动应扩展为Event而不是MenuEvent

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

只要改变这一点:

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

这样:

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

准确,如果u有一个事件定义的静态常量,然后使用它无处不在。 – JTtheGeek 2011-04-27 21:23:58

+0

Doh,忘了将MenuEvent改为Event(有没有用MenuEvent搞乱) - 但我以前就是这样。 Doh^2没有看到我使用“Update”字符串而不是事件中定义的静态常量字符串。 - 有时我可以很简单,你只是看不到它...会尝试和报告。非常感谢!如预期的那样, – masi 2011-04-28 07:23:03

+0

工作。 thx再次! – masi 2011-04-28 10:42:29