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
Suppose you have another event class (next to the class that is specified in the example):
public class MyEvent2 extends Event {
public static const MY_EVENT_TYPE:String = "myEventType";
}
Then the listener has no way of finding out if an incoming event is an instance of MyEvent or of MyEvent2.
Would it be an idea of making the event class an attribute of the Listener class? Or do I miss some important point?
I'll tell Nahuel to check the bug.
Eugene,
That's the way event listeners work in Flex, they only go by the event type, regardless of the class.
I understand that you are proposing a feature for this tag, but I don't think we would implement it, as it will add overhead, or you can try convincing Nahuel ;)
You can read about the best practices for events here: http://mate.asfusion.com/page/documentation/best-practices/events.
Is there an update on this bug?
I have also done exactly like you suggested:
---------------------------
myListener:Listener = new Listener()
myListener.method = your_method;
myListener.type = MyEvent.MY_TYPE;
Then to remove it:
myListener.removeEventListener(MyEvent.MY_TYPE, your_method);
---------------------------
However the removeEventListener failed to remove the event.
Only after I did myListener.type = null per Mark suggestion, the eventListener was properly removed.
Regards!
Yes, it does. That's why it has been deprecated to favor ListenerInjector. As soon as I add the docs for ListenerInjector, I'll mark this one deprecated.