If you apply the same listener twice, it will only fire once.
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.
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!!
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.
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!!