2011-04-27 74 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