3 Replies Latest reply on Jul 12, 2007 1:28 PM by marleeffh

    Event Dispatch Problem With Classes

    marleeffh
      I have a custom Timer class that pops up an inactivity alert after 3 minutes. The user can then choose 'yes' if they are still editing a record or 'no' if they are done. If they do not respond within 2 minutes of the alert it assumes 'no'.

      A 'yes' refreshes the timer and closes the alert.
      A 'no' kicks them out of the edit state in the component that the timer is called from.

      The logic of the timer is working fine but it doesn't seem to be sending the event to the component that called it so clicking 'no' (or remaining inactive) does nothing. I feel like I'm missing something really simple or just misunderstanding the eventDispatcher function. Advice please??

      Code From LockTimer class:

      public class LockTimer extends Sprite
      {
      private var threeMinuteTimer:Timer;
      private var twoMinuteTimer:Timer;

      public function LockTimer()
      {
      // creates a new threeMinute Timer
      threeMinuteTimer = new Timer(1000, 180);
      twoMinuteTimer = new Timer(1000, 120);

      // designates listeners for the interval and completion events
      threeMinuteTimer.addEventListener(TimerEvent.TIMER, onTick);
      threeMinuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

      twoMinuteTimer.addEventListener(TimerEvent.TIMER, onTick);
      twoMinuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteB);

      // starts the timer ticking
      threeMinuteTimer.start();
      }

      public function refresh():void{
      restart(threeMinuteTimer);
      twoMinuteTimer.reset();
      }
      private function onTick(evt:TimerEvent):void{}

      private function onTimerComplete(evt:TimerEvent):void{
      //trace("first timer");
      Alert.show('You have been inactive for several minutes. Are you still editing this record?', 'Inactivity Alert', Alert.YES|Alert.NO, null, alertListener,null, Alert.YES);
      twoMinuteTimer.start();
      }

      private function onTimerCompleteB(evt:TimerEvent):void{
      //trace('second timer');
      //Alert.show('TIME UP', 'Inactivity Alert', Alert.OK);
      dispatchEvent(new Event("timeUp",true));
      }

      private function restart(tim:Timer):void{
      tim.reset();
      tim.start();
      }


      private function alertListener(eventObj:CloseEvent):void{
      // Check to see if the YES button was pressed.
      if (eventObj.detail==Alert.YES) {
      refresh();
      }else{
      //exit edit mode
      //Alert.show('TIME UP', 'Inactivity Alert', Alert.OK);
      dispatchEvent(new Event("timeUp",true));
      }
      }
      }

      Code From Component:

      private var theTimer:LockTimer;
      private function initializeHandler(event:Event):void{
      addEventListener("timeUp",timeUpHandler);
      }
      private function timeUpHandler(event:Event):void{
      Alert.show('TIME UP', 'Inactivity Alert', Alert.OK);
      //editDone();
      }
        • 1. Re: Event Dispatch Problem With Classes
          ntsiii Level 3
          How are you instantiating the LockTimer instance? It must be a child of the component to receive bubbled events.

          Tracy
          • 2. Event Dispatch Problem With Classes
            marleeffh Level 1
            Code From Component:

            //declare the LockTimer
            private var theTimer:LockTimer;
            private function initializeHandler(event:Event):void{
            addEventListener("timeUp",timeUpHandler);
            }
            private function timeUpHandler(event:Event):void{
            Alert.show('TIME UP', 'Inactivity Alert', Alert.OK);
            //editDone();
            }
            ...


            and then later in the component i call the following function to initiate the LockTimer:

            private function editIt():void{
            //see if it's being edited
            if(checkLock()){
            //if its being edited send a message
            Alert.show('This record is currently being edited by another user. Please try again later.', 'Record Locked', mx.controls.Alert.OK);
            }else{
            theTimer=new LockTimer();
            refreshLock(1);
            form1.send();
            currentState="AdminEdit";
            }
            }
            • 3. Re: Event Dispatch Problem With Classes
              marleeffh Level 1
              so here is what i had to do to make this work:

              1. I had to attach the listener to theTimer rather than the component
              2. I had to initiate the LockTimer constructor outside of the function
              3. I had to call the handler initialization function from the component MXML

              below is the revised code (with excess cut out)

              <?xml version="1.0" encoding="utf-8"?>
              <mx:HDividedBox xmlns:mx=" http://www.adobe.com/2006/mxml" width="780" height="515" liveDragging="true" creationComplete="initializeHandler()">
              ......
              ......
              ......
              <mx:Script>
              <![CDATA[
              import mx.controls.Text;
              import mx.rpc.events.FaultEvent;
              import mx.rpc.events.ResultEvent;
              import mx.controls.Alert;
              import classes.LockTimer;

              public static const millisecondsPerMinute:int = 1000 * 60;
              public static const millisecondsPerHour:int = 1000 * 60 * 60;
              public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

              //IMPORTANT!!!////////////////
              private var theTimer:LockTimer=new LockTimer();
              private function initializeHandler():void{
              theTimer.addEventListener("timeUp",timeUpHandler);
              }
              private function timeUpHandler(event:Event):void{
              editDone();
              }

              private function refreshLock(theLock:int):void{

              var theTime:Date=new Date();
              if(theLock==1){
              theTimer.refresh();
              theTime.setTime(theTime.getTime() + (millisecondsPerMinute*5));
              }else if(theLock==0){
              theTime.setTime(theTime.getTime());
              }else{
              return;
              }
              form1.request.firmlockedTime=String(theTime.valueOf());
              form1.request.firmisLocked=String(theLock);
              }


              private function editIt():void{
              //see if it's being edited
              if(checkLock()){
              //if its being edited send a message
              Alert.show('This record is currently being edited by another user. Please try again later.', 'Record Locked', mx.controls.Alert.OK);
              }else{
              theTimer.startMe();
              refreshLock(1);
              form1.send();
              currentState="AdminEdit";
              }
              }
              private function editDone():void{
              refreshLock(0);
              form1.send();
              currentState="Admin";
              }

              ]]>
              </mx:Script>
              ......
              ......
              ......
              </mx:HDividedBox>