5 Replies Latest reply on Aug 27, 2010 9:54 AM by Flex harUI

    All events must have unique name?

    tzejing

      Hi

       

      Is it true that the all event names must be unique?

       

      For example, the custom event name "add" is invalid since there is already a FlexEvent.ADD ("add") defined.

      A listener registered for the event "add" will be triggered when there is any event with the same name is dispatched, right?

        • 1. Re: All events must have unique name?
          John Hall Level 4

          I don't think that would be true if you have custom events like MY_APP_ADD but I imagine if you're using "add" as a string for an event, that might conflict. Is there a major problem using some other string besides 'add'?

          • 2. Re: All events must have unique name?
            tzejing Level 1

            Yes, using "add" might cause conflict with the event using the same name "add". So in this case, the event name has to be unique right?

             

            I can always choose another name and try to make it very unique by adding prefix like "com.xyz.component.add", but that doesn't guarantee that the name is going to be unique in the future ...

            • 3. Re: All events must have unique name?
              Flex harUI Adobe Employee

              Only bubbling events need unique names, and most events shouldn't bubble.

              You do need to stay away from existing Flash events that bubble though.

              • 4. Re: All events must have unique name?
                tzejing Level 1

                I have a custom event MyEvent like the following:

                 

                package
                {
                     import flash.events.Event;
                
                     public class MyEvent extends Event
                     {
                          public static const ADD:String = "add";
                          
                          public function MyEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
                          {
                               super(type, bubbles, cancelable);
                          }
                          
                          override public function clone():Event
                          {
                               var event:MyEvent = new MyEvent(type, bubbles, cancelable);
                               return event;
                          }
                     }
                }
                

                 

                 

                Then I have a MyComp.mxml to dispatch a non-bubble MyEvent when a button is clicked:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx"
                           xmlns:local="*">
                     <fx:Metadata>
                          [Event(type="MyEvent", name="add")]
                     </fx:Metadata>
                     <fx:Script>
                          <![CDATA[
                               protected function submit(event:MouseEvent):void
                               {
                                    dispatchEvent(new MyEvent(MyEvent.ADD));
                               }
                          ]]>
                     </fx:Script>
                     <s:Button label="OK" click="submit(event)" />
                </s:BorderContainer>
                

                 

                 

                Then in my application, I registered an handler for MyEvent like the following:

                 

                <?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"
                                  xmlns:local="*">
                     <fx:Script>
                          <![CDATA[
                               protected function something(event:MyEvent):void
                               {
                               }
                          ]]>
                     </fx:Script>
                     <local:MyComp add="something(event)" />
                </s:Application>
                

                 

                 

                When run, the application throws the following exception before the screen is shown. It looks like the Flex failed to cast FlexEvent to MyEvent ...

                 

                TypeError: Error #1034: Type Coercion failed: cannot convert mx.events::FlexEvent@39421c9 to MyEvent.
                     at flash.events::EventDispatcher/dispatchEventFunction()
                     at flash.events::EventDispatcher/dispatchEvent()
                     at mx.core::UIComponent/dispatchEvent()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:12528]
                     at spark.components::Group/http://www.adobe.com/2006/flex/mx/internal::elementAdded()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:1428]
                     at spark.components::Group/setMXMLContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:512]
                     at spark.components::Group/set mxmlContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:452]
                     at spark.components::SkinnableContainer/set mxmlContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:604]
                     at spark.components::SkinnableContainer/createDeferredContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:986]
                     at spark.components::SkinnableContainer/createContentIfNeeded()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:1014]
                     at spark.components::SkinnableContainer/createChildren()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:827]
                     at mx.core::UIComponent/initialize()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7349]
                     at spark.components::Application/initialize()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Application.as:916]
                     at MyApp/initialize()
                     at mx.managers.systemClasses::ChildManager/childAdded()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\systemClasses\ChildManager.as:189]
                     at mx.managers.systemClasses::ChildManager/initializeTopLevelWindow()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\systemClasses\ChildManager.as:341]
                     at mx.managers::SystemManager/initializeTopLevelWindow()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\SystemManager.as:2810]
                     at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::kickOff()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\SystemManager.as:2637]
                     at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::preloader_completeHandler()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\SystemManager.as:2539]
                     at flash.events::EventDispatcher/dispatchEventFunction()
                     at flash.events::EventDispatcher/dispatchEvent()
                     at mx.preloaders::Preloader/timerHandler()[E:\dev\4.x\frameworks\projects\framework\src\mx\preloaders\Preloader.as:515]
                     at flash.utils::Timer/_timerDispatch()
                     at flash.utils::Timer/tick()
                
                The problem is gone if I set MyEvent.ADD to something else other than "add".

                • 5. Re: All events must have unique name?
                  Flex harUI Adobe Employee

                  You are subclassing BorderContainer which already has an ADD event, so you

                  will need to have a name that already isn't taken by the super classes.  My

                  point was that you don't' have to worry about other classes dispatching

                  whatever you choose for your event name unless they are bubbling.