So I was loving the MVC framework Mate, but started to run into issues trying to figure out best-practices for dispatching and handling events.
Researching:
1) How you dispatch events from non-view parts of your Flex Mate app
2) Is this a good practice and what is the best practice for using events in Mate with regards to loading an app’s data from a remote source and handling server data?
Results:
1) In a non-DisplayObject, don’t just use dispatchevent(), but you need to instantiate a Mate dispatcher.
At the top make sure you import the class.
import com.asfusion.mate.events.Dispatcher;
Then:
var myDispatcher:Dispatcher = new Dispatcher();
var qlEvt:QuotesLoadedEvent = new QuotesLoadedEvent(QuotesLoadedEvent.QUOTES_LOADED, true);
myDispatcher.dispatchEvent(qlEvt);
This worked for me. I was dispatching the event from a QuoteManager that was responding to the result of a RemoteObject call. But it seems that the listening component (a Box with a datagrid) would only hear the event when I did the following in an injector tag (target=”{QuoteGrid}”) in the eventMap:
<ListenerInjector method="resetQuotes" eventType="{QuotesLoadedEvent.QUOTES_LOADED}"/>
If I used:
<EventHandlers type="{QuotesLoadedEvent.QUOTES_LOADED}">
<MethodInvoker generator="{QuoteGrid}" method="resetQuotes"/>
</EventHandlers>
The QuoteGrid component did not hear the event.
2)TBD
See: http://mate.asfusion.com/page/documentation/tags/dispatcher
The main issue was dealing with events that were being launched from a Manager class, which was handling a remote response from php. After the response, in the manager, I was firing a custom Event that signaled the initial data had been loaded for the app, and then trying to listen for this data_loaded event to signal to the view to update the display (wasn’t a simple property injection since the display had filters, etc. which was making the data layer far more complex with a filteredArray, etc.).
This post: http://mate.asfusion.com/forums/topic.php?id=317 was helpful, when I realized why the hell my eventmap was not able to assign a responsive listener for the data_loaded event. The event was being fired from a Manager class, but nothing was listening or responding and I guess that is because the event map doesn’t handle events dispatched from non-DisplayObjects (like my QuoteManager).
So what is the best practice for notifying views that data has been loaded, when I cannot relying on simple property injection to get the data from the Manager to the view? TBD. Also, is injecting a listener int the eventMap the same as handling an event and invoking a method in the eventMap?
I want to 1) get a bunch of stock quotes for a dataGrid 2)After the data has been received, send it to the view, and not just update a property on the view but copy the data into various other properties so I can manipulate the data on the view with several filters and preserve the original state of the master ArrayCollection. I wonder if using expanded getters/setters on the original/master stock ArrayCollection and its source will be necessary. I would love to see some clear documentation on manipulating/filtering ArrayCollections and what happens to the Array source when the filter function is set.
I would also like to figure out a best-practice for handling server data, and responding to this data using the model concept of MVC and events in Mate. The documentation so far has been good, but not clear on how to accomplish the aforementioned goal.