2009-07-24 14 views
2

私は選択可能なコントロール(ラジオボタン) とテキスト入力で構成されたカスタムコンポーネントを持っています。これらのコントロールの両方からの 変更イベントに応答してロジックを実行したいが、それ以降は、コンポジットコンポーネントの変更ハンドラ に登録されているものにイベントを処理するための変更があるようにしたい。 問題は、 イベントを再ディスパッチして、イベントターゲットが自分のカスタム コンポーネントに変更され、元のイベントのターゲットが失われてしまうことです。複合カスタムコンポーネントでのFlex 3イベントの伝播?

はここに私のカスタムコンポーネントです:

<?xml version="1.0" encoding="utf-8"?> 
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" label="{[email protected]}" data="{[email protected]()}"> 

    <mx:Script> 
     <![CDATA[ 
      import mx.controls.RadioButtonGroup; 
      [Bindable] 
      public var selected: Boolean; 
      [Bindable] 
      public var text: String; 
      [Bindable] 
      public var listItem: XML; 
      [Bindable] 
      public var group: RadioButtonGroup; 

      private function onSelectionChange(event: Event): void { 
       selected = event.target.selected; 
       dispatchEvent(event); 
      } 

      private function onTextChange(event: Event): void { 
       text = event.target.text; 
       dispatchEvent(event); 
      } 
     ]]> 
    </mx:Script> 

    <mx:RadioButton group="{group}" label="{label}" selected="{selected}" change="onSelectionChange(event)"/> 
    <mx:TextInput width="100%" 
        maxChars="{[email protected]th}" 
        enabled="{selected}" 
        visible="{listItem.hasOwnProperty('specify')}" 
        includeInLayout="{visible}" 
        change="onTextChange(event)"/> 
</mx:HBox> 

このコンポーネントからイベントを変更する受信イベントハンドラで、私はevent.targetとして、SpecifyRadioButtonのインスタンスではなく、 のTextInputやラジオボタンで表示さ I期待している。 イベントをどのように伝えて、私がここで必要なものを手に入れるのですか?再派遣元のイベントは、新しいイベントを作成し、origEventプロパティとして元のイベントを渡す代わりに

Getting event [Event type="change" bubbles=false cancelable=false eventPhase=2] 
from question0.tabSurvey.questionForm.questionContainer.Single94.VBox95.SpecifyRadioButton111 

答えて

3

。 SpecifyRadioButtonが送出する新しいイベントは、Eventを拡張するカスタムイベントクラスであるか、遅延してmx.events.DynamicEventだけを使用することができます。

例:

import mx.events.DynamicEvent; 

private function onSelectionChange(event: Event): void { 
    selected = event.target.selected; 
    var newEvent:DynamicEvent = new DynamicEvent(Event.CHANGE); 
    newEvent.origEvent = event; 
    dispatchEvent(newEvent); 
} 

その後、SpecifyRadioButton.changeイベントのあなたのハンドラでは、event.origEventプロパティを参照。

2

イベントのターゲットは、イベントをディスパッチしているので、SpecifyRadioButtonになります。

TextInputコンポーネントの "change"イベントは、バブルアップされないように設定されています。つまり、同じコンポーネント内のリスナーによって聴くことができます。変更イベントをバブルアップさせたい場合は、TextInputクラスを拡張する必要があります(または、Mateのような気の利いたものを使用する必要があります)。

package { 

    import flash.events.Event; 
    import mx.controls.TextInput; 

    public class CarbonatedTextInput extends TextInput { 
     public function CarbonatedTextInput() { 
      super(); 
      addEventListener(Event.CHANGE, forceBubbles); 
     } 

     /* 
      We have to remove the event listener before we 
      dispatch the new event (with bubbles!) because 
      otherwise our listener in the constructor would 
      catch us and put us in an endless loop... 
      resulting in a **STACK OVERFLOW** 
     */ 
     protected function forceBubbles(e:Event):void { 
      removeEventListener(Event.CHANGE, forceBubbles); 
      dispatchEvent(new Event(Event.CHANGE, true)); 
      addEventListener(Event.CHANGE, forceBubbles); 
     } 
    } 
}