5 Replies Latest reply on Oct 29, 2010 3:15 AM by luke3design!!

    hasEventListener and willTrigger always return true

    luke3design!! Level 1

      Hi  everybody.

      I'm trying to achieve the following result.

      I'm listing all items in a form.

      When I encounter a button I check if it has already an event listener applied to it. If not , I apply the event Listener with addEventListener.

      In this way, the next time I'll list the whole form I won't apply the event listener another time (if for example I apply the same event listener twice to a button which insert a new record in a database as result I'll have 2 records added also if the user clicked once...no good at all!!!)

      To accomplish this task I'm using the Actionscript function hasEventListener(EventType) and here comes the weird thing.

      Eveytime I invoke hasEventListener on the form's button it always returns true event if is the first time a list the form (so no event listener applied yet) , even if before to invoke hasEventListener I run removeEventListener.

      I've got the same behavior when i use willTrigger() function, it always returns true no matter when i call it.

       

      One possible solution could be set a global boolean property to store if Ialready listed the form and applied the event listener to it, but it would be better using hasEventListener, because I've got a class that perform this form listing , and this class is used on wide project, so if I get to fix this problem inside the class I would fix the problem in the entire project in on shot (I love oop programming!!!!)

       

      Has anybody already addressed this problem?

      Any advisement would be really appreciated.

      Thanks in advance for the attention.

      Best regards!!

      Luke

        • 1. Re: hasEventListener and willTrigger always return true
          Flex harUI Adobe Employee

          If you apply the same listener twice, it will only fire once.

          • 2. Re: hasEventListener and willTrigger always return true
            luke3design!! Level 1

            Hi, flex harUI.

            Thanks very much for your reply.

            I knew that if I apply an event listener twice it should fire up only once, but this isn't what happen.

             

            Here's my scenario:

            -  I've got a component with the form inside it

            - Through a menu a go to the state the component is in and I call an "init" function inside the component

            - In the init function I call the Class which activate the form by applying eventlistener on the button

            - The user can navigate away from the component by click another menu item and then return to the component invoking again the form activation (and so let my application apply once again the addEventListener)

            - Here's what happen: the second,third,... time the user return to the component, when he try - for example - to add a record, the record is added two,three... times according to how many times the users "reinitialized" the component. The same things happen when the user tries to delete a record, he is prompted two,three,.... times if he's sure to delete it.....

             

            It's a really strange behaviour.

             

            I've found i worked around, by setting a global bool var "initialized" and before call the form activation i check the value of this var, but It's not a very beautiful solution, 'cause I would prefer to fix this problem inside the class, so the fix the problem in the whole project just in one shot!!! (that's why I would like to use hasEventListener() to check if the button has already an eventListener)

             

            Thaks anyway for the reply!

            If I've some news I'll update ya.

             

            Bye!

            • 3. Re: hasEventListener and willTrigger always return true
              luke3design!! Level 1

              Here I am with some updates.

              I didn't fix the hasEventListener issue yet, but I worked out a quite smart solution.

              I put the call to the form activator into a function that is called at the component initialization and it works.

              It's more the solution to the "I want to call that function just once" problem  than the solution to "the hasEventListener doesn't work properly" , but it works.

               

              I've also tried to call once again tha hasEventListener (as Murphy Law's says: "what's impossible on Friday it will be easily solved on monday", but I'm in an exception :-(   ), but it still returns true. The mistery goes on.

               

              If someone had already a similar problem pleas share your experience!!

              Regards!

              Luke

              • 4. Re: hasEventListener and willTrigger always return true
                Flex harUI Adobe Employee

                Maybe you are adding anonymous functions as event listeners?  I am not a fan

                of doing that.  It is tricky to remove the listener.  Adding class instance

                methods is preferred, then multiple calls to add won't accumulate.

                • 5. Re: hasEventListener and willTrigger always return true
                  luke3design!! Level 1

                  No I'm not adding anonymous function, I hate doing it too!!!

                  I always use addEventListener(EventToListen,function_declared_somewhere_in_my_class)

                   

                  Thanks anyway 4 the tips!!

                  Bye!

                  Luke