Question

I've recently changed to Mate as a framework for flex. However am running into a few issues. I want to know how to dispatch events from classes generated via the methodInvoker tag.

<resultHandlers>
        <MethodInvoker generator="{LoginSuccess}" method="setCurrentUser" arguments="{[resultObject]}"/>

Then in the class I'd like to dispatch an event.

public function setCurrentUser(calUser:Object):void{
        if(calUser != null){
            Model.instance.currentUser = calUser as CalUser;
            loadOnlineCalendars(Model.instance.currentUser);
        }
    }

    public function loadOnlineCalendars(calUser:CalUser):void{
        for(var i:int = 0 ; i<calUser.calConnectors.length; i++){//logic here
            dispatchEvent(new CalConnectorEvent(calUser.calConnectors as CalConnector,CalConnectorEvent.LOAD_ONLINE_CALENDAR_EVENT,true));
        }
    }

The problem is I can't seem to be able to catch the event in the eventMap or even in the application root.

If anyone has any experience with Mate, I'd appreciate some pointers. Perhaps I'm doing this all wrong. I just want to get a result from the server - look at the result and depending on the result contact the server again. Should be quite simple.

Event Map:

            <resultHandlers>
            <MethodInvoker generator="{LoginSuccess}" method="setCurrentUser" arguments="{[resultObject]}"/>
            <ServiceResponseAnnouncer type="result"/>

            </resultHandlers>
            <faultHandlers>
            <ServiceResponseAnnouncer type="fault"/>

            </faultHandlers>
    </RemoteObjectInvoker>

Was it helpful?

Solution

The way it's usually done is to inject the event map's dispatcher into the object:

<MethodInvoker generator="{MyClass}" method="someMethod" arguments="{[a, b]}">
  <Properties dispatcher="{scope.dispatcher}"/>
</MethodInvoker>

The inner Properties tag sets properties on the object being created by the MethodInvoker, and the properties are guaranteed to be set before the method is invoked.

The class obviously needs to have a public property called dispatcher (or whatever name you prefer) for this to work. To dispatch events that you want to listen for in the event map call dispatcher.dispatchEvent(...).

If the object created by the MethodInvoker will be used more than once, if it's a manager, say, the common idiom is to create it using an ObjectBuilder is an event handler block that gets triggered by FlexEvent.INITIALIZE:

<EventHandlers type="{FlexEvent.INITIALIZE}">
  <ObjectBuilder generator="{MyClass}" constructorArguments="{scope.dispatcher}"/>
</EventHandlers>

In this example the event dispatcher is injected as a constructor argument, but you can use an inner Properties tag just as with MethodInvoker.

OTHER TIPS

After some digging around here, I found that you can't call an event from a non-view class. That forum post describes the elegant solution, and also offers a quick workaround:

Application.application.dispatchEvent(new CalConnectorEvent(calUser.calConnectors as CalConnector,CalConnectorEvent.LOAD_ONLINE_CALENDAR_EVENT,true));

But check out the forum post- there's a lot of meat in there.

From the class in actionscript you can dispatch events. For that it has to either extend the EventDispatcher class or use some dispatcher property and implement the IEventInterface. But that is not the proper way because the event will not be bubbled, since bubbling is a property specific to display objects. You can listen to the event in the class itself during the target phase. If you want other nodes to listen to the event it has to bubble. So as Theo has answered you have to pass the dispatcher of the mate framework to the class. Now you can dispatch the event from the class. This is probably the best way to dispatch events from the data classes. For more info refer to this link http://www.developria.com/2010/05/pass-the-eventdispatcher-pleas.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top