Listener

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)" />

9 responses

  1. How do you create a Mate listener in AS? I'd like to enable and disable the listener dynamically. I figured I would do this by dynamically adding the listener and removing it when needed.
  2. Hi Mike,
    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);
  3. Hi,

    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
  4. Because the Listener tag doesn't let you specify the Event class, the event type ("myEventType" in the example) has to be globally unique, or at least unique among all events that can be received by the Listener instance.

    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?
  5. Mark,
    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.
  6. Hi Laura,

    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!
  7. does this not tie the view a bit too much into having to use mate? is not better to use injectors rather? or would you use this in a app that used remoteobject and messaging?
  8. ryan,
    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.
  9. Extend mate listener and in the constructor add a new listener that sets type to null. Dispatch the event from anywhere to remove listeners. Setting type of event to null and useWeakReference to true should mark it for garbage collection.

    -------------------------------------------
    super();
    mateListener = new Listener;   
    mateListener.type=MyEvents.REMOVE_MATE_LISTENERS;         mateListener.method=removeMateListener;

    -------------------------------------------

    private function removeMateListener(event:MyEvents):void {
    this.type=null;
    mateListener.type=null;
    }

Comments now closed