Mate – Problems Dispatching Events from a Manager Class

February 4th, 2009

I switched to Mate framework for Flex a few months ago and really love its simplicity in respect to Cairngorm and other frameworks. Mate is a tag-based, event-driven Flex framework that provides a mechanism for dependency injection to make it easy for the different parts of an application to get the data and objects they need. If you know Spring then you’ll love Mate.

In this project I’m using BlazeDS for remoting and messaging with Salesforce.com and SQL Server 2005.

With Mate, you typically have a view (ie Login.mxml) that dispatches an event (ie LoginEvent.DO_LOGIN) and an EventMap that listens for the event and performs some action (eg log into Salesforce.com). My problem was that I was using an inline itemrenderer in my view which was making it extremely difficult to process and dispatch the event. My proposed solution was to dispatch a simple event and have the EventMap invoke a method on my Manager:

1
2
3
4
 
<eventHandlers type="{LoginEvent.DO_LOGIN}">
    <methodInvoker generator="{SfdcManager}" method="doLogin"/>
</eventHandlers>

The Manager method would then process the login request based upon some business logic and then dispatch appropriate event:

1
2
3
4
5
6
7
8
9
10
11
12
 
public function doLogin():void {
 
    // process some business logic
 
    // dispatch the sfdc login event
    var evt:LoginEvent = new LoginEvent(LoginEvent.DO_SFDC_LOGIN);
    evt.username = "my_username";
    evt.password = "my_password";
    dispatchEvent(evt);
 
}

Unfortunately this approach does not work as events dispatched from non-views will not make it to the event map. I found a very detailed post of the process and a proposed work around. There was also another post with some more information that helped me successfully dispatch my event from the Manager class.

Here is the event handler in the map injecting the event map’s dispatcher into the Manager:

1
2
3
4
5
6
 
<eventHandlers type="{LoginEvent.DO_LOGIN}">
    <methodInvoker generator="{SfdcManager}" method="doLogin">
        <properties dispatcher="{scope.dispatcher}"/>
    </methodInvoker>
</eventHandlers>

In the Manager class we need to add a public property for the dispatcher reference:

1
2
 
[Bindable] public var dispatcher:GlobalDispatcher;

Now modify the method to dispatch events using the reference to the dispatcher public property (ie dispatcher.dispatchEvent(evt)):

1
2
3
4
5
6
7
8
9
10
11
12
 
public function doLogin():void {
 
    // process some business logic
 
    // dispatch the sfdc login event
    var evt:LoginEvent = new LoginEvent(LoginEvent.DO_SFDC_LOGIN);
    evt.username = "my_username";
    evt.password = "my_password";
    dispatcher.dispatchEvent(evt);
 
}

Now you can successfully dispatch an event from a Manager class and have it picked up from in the EventMap.

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Mate - Problems Dispatching Events from a Manager Class, 10.0 out of 10 based on 1 rating

Categories: Flex, Salesforce

Leave a comment

Comments Feed5 Comments

  1. Tcoz

    Don’t forget to instantiate the instance of the dispatcher (simply adding the reference didn’t work for me).

    In constructor of the manager:

    dispatcher = new GlobalDispatcher ( );

    This is a dubious thing for me: I appreciate that Mate advocates itself saying “uses Flash events”, but it wants you to use them in a very particular way, unlike say PureMVC, where you add listeners and dispatch events exactly as you would without the framework. I don’t want to open a debate about the merits of Mate, I am liking working with it, but it does indeed alter how you would typically think about using events.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. ivelnal

    thanks for this good post.

    I’m new with mate and I’m planning to use it as our main framework.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  3. Vinay

    Thats Working!
    However if you want to dispatch an event from the view function (Fuction which listens to the event dispatched by Manager) does not listens by the Manager.
    Means
    Event Dispaching is like
    1) View->EventMap->Manager fine
    2) Manager->EventMap->View fine
    3)View->EventMap->Manager (not working )

    any workaround on this?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  4. kjv007

    Thanks for sharing info.

    another work around would be extend GlobalDispatcher to your manager class.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  5. stephen

    @Vinay
    (it’s been a long time, but here’s a pointer for anyone with this issue)

    See Mate’s InjectorRegistry.register

    “…registers a target instance to be used by the InjectorHandlers to inject any properties on it.”

    http://mate.asfusion.com/api_docs/com/asfusion/mate/ioc/InjectorRegistry.html

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

http://blog.jeffdouglas.com / Mate – Problems Dispatching Events from a Manager Class