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.

  • Share/Bookmark

Related posts:

  1. Handling asynchronous Flex calls to Salesforce.com using an MVC framework
  2. Returning Contacts and Leads with Custom Wrapper Class
  3. Admin to Hero App Building Workshop — Mission Accomplished!
  4. Tutorial: Salesforce.com with Flex and BlazeDS
  5. ActionSupport Example using Facets and JavaScript

Categories: Flex, Salesforce

Leave a comment

Comments Feed3 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.

  2. ivelnal

    thanks for this good post.

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

  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?

Leave a comment

Feed

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