2009-07-24 4 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]}" 
        enabled="{selected}" 
        visible="{listItem.hasOwnProperty('specify')}" 
        includeInLayout="{visible}" 
        change="onTextChange(event)"/> 
</mx:HBox> 

이 구성 요소에서 이벤트를 변경받는 이벤트 처리기에서, 그 event.target 같이, SpecifyRadioButton의 인스턴스가 아닌 의 TextInput 또는의 RadioButton입니다 참조 I 기대하고있어. 이벤트를 전파하여 내가 원하는 것을 얻으려면 어떻게해야합니까? 다시 파견 원래 이벤트는 새 이벤트를 만들고 origEvent 속성으로 원래 이벤트를 전달하는 대신

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

답변

3

. SpecifyRadioButton이 전달하는 새 이벤트는 Event를 확장하는 사용자 정의 이벤트 클래스 일 수 있습니다. 그렇지 않으면 lazy가되어 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); 
     } 
    } 
} 
관련 문제