This tag has been deprecated, use ListenerInjector instead.
Listener allows you to register a view as a listener for an event type. As long as the event bubbles up or is dispatched via the mate:Dispatcher tag or class, the registered listeners will be notified.
Example:
<mate:Listener type="myEventType" method="handleThisEvent" />
or
<mate:Listener type="myEventType" receive="handleThisEvent(event)" />
To handle the event received, you can use the method attribute or the receive attribute. The above examples accomplish exactly the same goal.
If the dispatcher of the event is a child component, we could do the same in ActionScript:
addEventListener("myEventType", handleThisEvent);
Note, however, that this will only work for events that are bubbling up from children components. The Listener tag, on the other hand, allows you to listen to events dispatched anywhere in your application, even from views contained in PopUp windows. Note: for PopUps, views must use the Dispatcher tag.
Attributes
type
required
The type attributes specifies the type of event for which we would like to register.
Suppose you have an event class definition as follows:
public class MyEvent extends Event {
public static const MY_EVENT_TYPE:String = "myEventType";
}
You can then specify the type attribute with the event type literal string:
<mate:Listener type="myEventType" method="handleThisEvent" />
Or you can use the binding syntax:
<mate:Listener type="{MyEvent.MY_EVENT_TYPE}" method="handleThisEvent" />
This will allow the compiler to check that the type you specified exists.
method
either method or receive must be provided
The method attribute specifies the method to call when an event is received. Called method will automatically receive the event.
If you have a listener tag as follows:
<mate:Listener type="myEventType" method="handleThisEvent" />
You will need to create a method called "handleThisEvent" that will be called when an event of type "myEventType" is received.
private function handleThisEvent(event:MyEvent):void {
// handle the event
}
receive
either method or receive must be provided
The receive event handler allows you to handle the event inline. In this attribute you can write simple ActionScript statements.
If, for example, you wanted to change the state of the view when an event is dispatcher, you could do that inline as in the following example:
<mate:Listener
type="myEventType"
receive="currentState='myOtherState'" />
In this inline handler you also have access to the event that was dispatched.
<mate:Listener
type="myEventType"
receive="trace(event.myProperty)" />
It is possible, I don't know if this is what you are doing:
myListener:Listener = new Listener()
myListener.method = your_method;
myListener.type = MyEvent.MY_TYPE;
Then to remove it:
myListener.removeEventListener(MyEvent.MY_TYPE, your_method);
I think there is a bug with removing the removeEventListener() method. It will not remove the listener unless the first argument is "receive" and not "MyEvent.MY_TYPE".
However, instead of using the removeEventListener() directly, you can set the type = null. This will essentially remove the listener from the global dispatcher and will thus work.
myListener.type = null