Mate is a is a tag-based, event-driven lightweight Flex framework that could be used mixed with others like Cairgorm. It uses injection to pass reference to the views declared in a central EventMap, where you also declare event handling. You have a lot of helper classes, like Mocks or different tags for invoke methods, but mate doesn’t impose a rigid structure.
Let’s take a very quick tour trough Mate fundamentals.
You can download the demo application as fxp file (Flash Builder Project) from here
You can see the running application here (select framework you’re working on for multiple choice)
Event map
Central to Mate is the Event Map. In the Event Map (or multiple event maps), you define what needs to happen when certain events are dispatched. Each event type you want to listen to will have its own Event Handlers block in the event map. Usually you declare it in the main application and use for handling events and inject dependencies. Here’s an example from our test application
<MethodInvoker generator=”{User}” method=”addResult” arguments=”{[event]}” />
<MethodInvoker generator=”{QuizzModel}” method=”moveNext” />
</EventHandlers>
<!– Services ->
<services:Services id=”services”/>
<!– Injectors –>
<Injectors target=”{LoginForm}”>
<PropertyInjector targetKey=”user” source=”{User}”/>
</Injectors>
Event Handlers
An Event Handlers block defined in the Event Map will run whenever an event of the type specified in the EventHandlers’ “type” argument is dispatched. In order to handle events, the bubbling phase should be set to true and should be dispatched from an object that has Application as his root. You can see an example of event handler in the previous code
MethodInvoker, WebServiceInvoker, HTTPServiceInvoker, RemoteObjectInvoker
Easy to understand by its names, all this tags are used to invoke methods or services usually inside an EventHandler.
methodInvoker is one of the most used, it will create an object of the class specified in the “generator” attribute. It will then call the function specified in the “method” attribute on the newly created object.
The others invokers depends on external services, so declare an resultEvent and a faultEvent to manage results. Here’s an example of a MethodInvoker
generator=”ClassNameToInstantiate”
method=”methodToExecute”
arguments=”{['argument1', 'argument2']}”/>
There are others handy tags like DataCopier (to copy data into some storage), StopHandlers (to prevent handlers to run at certain point) or MessageHandlers (for Flex Messaging system) between others.
Multiple choice using Mate
Being the second example of this story, I begin coping the files from the Swiz project directly to my new one, keeping some of the classes that probably will survive without much change among all the others frameworks, like Events classes or Models for quiz and User (in the case of the user basically a Data Transfer Object)
Steps were as follows
- Add the compiled framework code to your project (Mate.swc).
- Create a file that will be the EventMap, to declare injectors and event handlers
- Include the event map in your main Application file.
- Create one or more custom event.
- Create one or more views
- Dispatch that event from the different views as needed.
- Add EventHandlers in your event map that listen for the event type you dispatched.
- Execute some actions inside the EventHandlers block (ie: call the server, store data, etc).
Add as many event handlers as you need
Highligths
- The event map centralize the functionality and probably you will go back and forth to it as far as you add views
- A bunch of events is needed for different actions, create as much as you need
- If you use states for the main application be careful: move the EventMap tag to the first view, if not each time you move to another view it will be redeclared.
- Avoid any relationship between view and models (in the business folder), use injection instead trough the EventMap
- Use a Services.mxml to declare your external services.
- You can use Mocks for your service at development stage, just use a MockRemoteObject tag inside services and create your Mock class (inside mock.quiz.services package)
- For the clock we have used just a custom class that dispatch events and is injected into the view
- For the final result we collect all the info from the User, where we store progress.
Summarizing: Mate is an extremely easy framework with a centralized management, just avoid it if you don’t like mxml tags but prefer pure AS classes
For further reading on the topic: http://mate.asfusion.com/
