views:

353

answers:

2

In as3, if I add identical event listeners to an object, for examle

txtField.addEventlistener( Event.CHANGE, changeCb, false, 0, true );
txtField.addEventlistener( Event.CHANGE, changeCb, false, 0, true );

would I need to remove that listener twice?

How can I get a list or the number of event listeners on an object?

+1  A: 

No, you would not need to remove twice. You would only create one registration. Also, you are using weak references (by setting the last parameter, useWeakReferences to true). So that makes it even easier to reason about.

There is a section in the docs that describes the cases where you would create two listener registrations for the same listener function.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/EventDispatcher.html#addEventListener()

drudru
+2  A: 

No, you don't need to remove the listener twice in that situation.

You need to remove multiple listeners in two situations:

  1. if you add two event listeners with different listener functions:

    txtField.addEventlistener( Event.CHANGE, changeCb, false, 0, true );
    txtField.addEventlistener( Event.CHANGE, changeCb2, false, 0, true );

  1. if you set one event to fire in the capturing phase:

    txtField.addEventlistener( Event.CHANGE, changeCb, false, 0, true );
    txtField.addEventlistener( Event.CHANGE, changeCb, true, 0, true );

So you only need to remove events that are registered in a different manner from each other.

You can't get a count of the event listeners with what's provided out of the box in Flex, but you can check if it has an event listener for a specific type of event using the hasEventListener(type).

However, because the source code if provided, you can "Monkey patch" the UIComponent or the FlexSprite class to add this functionality, as explained in this blog. Actually, you don't even have to do it. Code is provided in the example. Pretty cool.

Mircea Grelus
Good to know. Thanks.
Michael Todd