2009년 8월 16일 일요일

event 나중에 처리하기

flexmdi라는 flex에 데스크탑을 구현한 오픈소스가 있다. 여기를 클릭하면 볼 수 있다. 여기 샘플 코드에 괜찮은 설명이 있어서 옮겨 적는다.

 

 

   // the flex framework dispatches all kinds of events
   // in order to avoid catching one of those and throwing a coercion error
   // have your listener accept Event and check the type inside the function
   // this is good practice for all Flex development, not specific to flexmdi
   private function confirmWindowClose(event:Event):void
   {
    if(event is MDIManagerEvent && confirmCloseCb.selected)
    {
     // store a copy of the event in case we want to resume later (user confirms their intention)
     queuedEvent = event.clone() as MDIManagerEvent;
     
     // this is the line that prevents the default behavior from executing as usual
     // because the default handler checks event.isDefaultPrevented()
     event.preventDefault();
     
     Alert.show("Seriously? Close it?", null, 3, null, handleAlertResponse);
    }
   }
   
   // called when the Alert window is closed
   // if the user said yes, we execute the default behavior of playing an effect
   // and then removing the window by sending the stored event to
   // the appropriately named executeDefaultBehavior() method
   private function handleAlertResponse(event:CloseEvent):void
   {
    if(event.detail == mx.controls.Alert.YES)
    {
     mdiCanvas.windowManager.executeDefaultBehavior(queuedEvent);
    }
   }

 

 

윈도우를 닫을 때 발생한 이벤트에서 이벤트를 clone하여 queuedEvent라는 곳에 임시 저장하고 event는 preventDefault() 한다. 만약 사용자가 윈도우를 닫기 원하면 queuedEvent를 처리한다.

 

이벤트를 받을 때 타입체크를 하라는 설명도 보인다.

댓글 없음:

댓글 쓰기