이벤트를 상속받아서 만들면 특정 데이터를 보낼 수 있다.

1. 커스텀 이벤트 생성

<< customEvent.as >>

---------------------------------------------------------------------------------------------------
package customEventTest
{
 import flash.events.Event;

 public class customEvent extends Event
 {
  //이벤트 타입 선언
  public static const CUSTOM_EVENT:String = "customEvent";
  //전달될 데이터 저장
  public var customEventData:Object;
 
  public function customEvent(type:String, customEventData:Object, bubbles:Boolean=false, cancelable:Boolean=false)
  {
   super(type, bubbles, cancelable);
   this.customEventData = customEventData;
  }
 
  override public function clone():Event
  {
   return new customEvent(type, customEventData, bubbles, cancelable);
  }
 }
}
---------------------------------------------------------------------------------------------------


2. 컴포넌트에서  이벤트를 사용할 수 있도록 metaData를 선언한다.

<< customCanvasAS.as >>

---------------------------------------------------------------------------------------------------
package customEventTest
{
 import mx.containers.Canvas;

 //customEvent metaData 설정
 [Event(name="customEvent", type="customEventTest.customEvent")]

 public class customCanvasAS extends Canvas
 {
  public function customCanvasAS()
  {
   super();
  }
 
 }
}
---------------------------------------------------------------------------------------------------


<< customCanvas.mxml >>

---------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300">
 
 <!-- mxml에서의 metaData 설정 -->
 <mx:Metadata>
  [Event(name="customEvent", type="customEventTest.customEvent")]
 </mx:Metadata>
 
</mx:Canvas>
---------------------------------------------------------------------------------------------------


3. 특정 이벤트 발생시 커스텀 이벤트 실행.

<< customEventTest.mxml >>

---------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
 xmlns:customEventTest="customEventTest.*"
 creationComplete="createApp();"
 >
 
 <mx:Script>
  <![CDATA[
   import customEventTest.customEvent;
   
   private function createApp():void
   {
    cusCnvAS.dispatchEvent(new customEvent(customEvent.CUSTOM_EVENT, {testData:"cusCnvAS customData"}));
    cusCnv.dispatchEvent(new customEvent(customEvent.CUSTOM_EVENT, {testData:"cusCnv customData"}));
   }
   
   private function customEventHandler(event:customEvent):void
   {
    trace(event.customEventData.testData);
   }
   
  ]]>
 </mx:Script>
 
 <customEventTest:customCanvasAS id="cusCnvAS" customEvent="customEventHandler(event);"/>
 
 <customEventTest:customCanvas id="cusCnv" customEvent="customEventHandler(event);"/>
 
</mx:Application>
---------------------------------------------------------------------------------------------------

위 customEventTest.mxml  creationComplete이벤트 발생시 customEvent를 dispatch 시켜서 이벤트를 발생시킬때 특정 데이터를 담아서 보내면 그 데이터를 받아서 어떠한 일을 처리 할 있다.
예를 들어 마우스 클릭 이벤트 발생시 해당 컴포넌트와 특정 데이터를 보낼때  컴포넌트와 데이터를 받는 이벤트를 만들어서 사용할 수 있을 것이다.

태그 : Metadata
  1. cliff3 2009/03/20 01:20 답글수정삭제

    주제를 정해서 하는게 도움이 많이 될거에요.
    무작정 api만 이해하는건... 별로. :-P
    (내가... 그래서 아는게 별로 없어요... ㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎㅎ. ㅠ.ㅠ)

  2. 2010/03/15 09:30 수정삭제

    비밀댓글 입니다

트랙백 주소 :: http://www.2su0.com/65/trackback/
옵션
댓글 달기
이전 1 ... 27 28 29 30 31 32 33 34 35 ... 81 다음