5 Replies Latest reply on Oct 21, 2010 5:26 AM by flex2008

    A question about overriding clone() method in a custom event.

    flex2008 Level 3

      I was preparing a presentation on events.As part of that I was trying out an application to illustrate the need for overriding clone() method.This is the sample code I was using:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                     xmlns:s="library://ns.adobe.com/flex/spark" 
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                     creationComplete="application1_creationCompleteHandler(event)">
          
          <fx:Script>
              <![CDATA[
                  import events.CustomEventNoCloning;
                  
                  import mx.events.FlexEvent;
                  protected function button1_clickHandler(event:MouseEvent):void
                  {
                      innerContainer.dispatchEvent(new CustomEventNoCloning(CustomEventNoCloning.NOT_CLONED,textinput.text,true));
                  }
                  
                  protected function application1_creationCompleteHandler(event:FlexEvent):void
                  {
                      parentComp.addEventListener(CustomEventNoCloning.NOT_CLONED,reDispatchedEventHandler);
                      parentComp.addEventListener(CustomEventNoCloning.NOT_CLONED,dispatchedEventHandler,true);
                  }
                  
                  private function reDispatchedEventHandler(event:CustomEventNoCloning):void
                  {
                      lbl.text = event.enteredText;
                  }
                  
                  private function dispatchedEventHandler(event:CustomEventNoCloning):void
                  {
                      parentComp.dispatchEvent(event);
                  }
                  
              ]]>
          </fx:Script>
          
          <s:BorderContainer id="parentComp" x="188" y="147" width="200" height="200">
              <s:BorderContainer id="innerContainer" x="21" y="30" width="157" height="136">
                  <s:TextInput id="textinput" x="13" y="43"/>
                  <s:Button x="42" y="76" label="Click" click="button1_clickHandler(event)"/>
              </s:BorderContainer>
          </s:BorderContainer>
          <s:Label id="lbl" x="236" y="367" width="119"/>
      </s:Application>
      
      

       

      This is the content of the custom event class:

       

      package events
      {
          import flash.events.Event;
          
          public class CustomEventNoCloning extends Event
          {
              private var _enteredText:String;
              public static const NOT_CLONED:String = 'notCloned';
              
              public function CustomEventNoCloning(type:String, text:String,bubbles:Boolean=false, cancelable:Boolean=false)
              {
                  super(type, bubbles, cancelable);
                  _enteredText = text;
              }
              
              public function get enteredText():String
              {
                  return _enteredText;
              }
          }
      }
      

       

      When I am clicking the button,type coercion exception occurs which is undetstandable since i have not overridden the clone() method.But What i could not understand was that the enteredText property was correctly shown in the label.How is it possible,since while redispatching,the clone() method in the Event class only will be exceuted?

        • 1. Re: A question about overriding clone() method in a custom event.
          BhaskerChari Level 4

          @flex2008,

           

          Its true as you said the type coercion exception occurs since you have not overridden the clone() method. But the thing here you should note is even though there is a type mismatch of the event type the property enteredText still exists on that event Object except the type of Event is normal flash Event.

           

          Hope you understood..Since the event you dispatched is infact a event with enteredText property...

           

           

          Just to let you know as you are giving a presentation on events. I would like you to highlight one point in your code...so that you can share..


          If in your dispatchedEventHandler() method if you dispatch the event with innerContainer instead of parentComp then your application will go into a Event dispatch recursion overflow error which means that your event gets dispatched and listened infinite times.

          private function dispatchedEventHandler(event:CustomEventNoCloning):void
                      {
                          innerContainer.dispatchEvent(event);
                      }

           

           

           

          Thanks,

          Bhasker

          • 2. Re: A question about overriding clone() method in a custom event.
            flex2008 Level 3

            Thanks for the reply.

             

            Still I am not convinced about why enteredText managed to come here.This will be the structure of the clone() method in this class:

             

            public function clone():Event
            {
                return new Event(type,bubbles,cancelable);
            } 
            

             

            What it does is that it basically creates a new Event instance with properties of the current event instance.Right?.Here,the event instance if of type CustomEventNoCloning,but the clone() method creates an Event instance,with the properties type,bubbles and cancelable only of the current CustomEventNoCloning instance.

             

            Am I missing something?

            • 3. Re: A question about overriding clone() method in a custom event.
              BhaskerChari Level 4

              @flex2008,

               

              Your clone method should look something like below based on the CustomEventNoCloning event class you posted:

               

              override public function clone():Event
                      {
                       return new CustomEventNoCloning(type, _enteredText, bubbles, cancelable);
                      }

               

               

              Thanks,

              Bhasker

              • 4. Re: A question about overriding clone() method in a custom event.
                flex2008 Level 3

                That is how i have to write if i am overriding the clone() method.Since i have not done that overriding,the clone() method will take the form i posted in my last comment.In that method,only the type,bubbles and cancelable properties will be taken from the current CustomEventNoCloning instance.