MyMVC, Adobe AIR (Flex) application framework

version 2.0


MyMVC source and binary | Documentation | Example


MyMVC is a framework for Adobe AIR (or Flex) applications. It is designed with the following principles (in addition to standard model-view-controller principles):

  1. Front controller should be able to register a sequence of commands against an event.
  2. Front controller should be able to register a parallel of commands against an event.
  3. Front controller can do "switching" work.
  4. A command has to dispatch "complete" event when its job is done. It dispatches "error" event if erros happened. If the process is lengthy, it may dispatch "progress" event.
  5. Event has "who" and "what" properties, indicating the object and function which dispatches the event.

Front controller can register sequence commands

Commands are like bricks and front controller, to build a complex building, should be able to combine them differently in different situations. This also makes individual commands reusable. MyMVC allows you to combine commands in a straightforward way. Below is some sample codes of controller in MyMVC:

addCommand('SHOW_ERROR', DisplayErrorCommand);
addCommand('REGISTER', [RegisterCommand,DisplayStatusCommand]);
addCommand('REGISTER5s', [FivSecCommand,RegisterCommand,DisplayStatusCommand]);

The first line is what you usually see in a regular MVC framework (e.g. Cairngorm) where a single command is registered against a single event. If you want to perform command A, then B, then C for event 'E', you will have to write a new command, say X, to perform A-B-C. In MyMVC, however, you simply write something like line 2 or 3. This way, each individual command class is highly reusable and they can be combined in whatever way you want without writing new command classes.

Front controller can register parallel commands

What if you want to repeat the same task 5 times, each time with different input data? For example, you might want to 'register' 5 different accounts. You can, for example, do:

addCommand('REGISTER', [RegisterCommand,DisplayStatusCommand]);

And then broadcast the 'REGISTER' event 5 times with different data. Or, you can do:

addParallelCommands('PARALLEL_REGISTER', [RegisterCommand,DisplayStatusCommand]);

When you broadcast 'PARALLEL_REGISTER' event, the property has to be an array. The Front Controller will automatically splits your data, and perform each data in sequence (i.e. register first user, then 2nd, so on).

Note, the 2nd argument of addParallelCommands has to be an Array. If you only want to input a single command, still use an Array (of length 1). e.g.

addParallelCommands('PARALLEL_REGISTER', [RegisterCommand]);

Commands should dispatch events

Linux commands are straightforward. They receive some input and then (maybe after a long time) spit out the result (output). This simplicity makes it not only useful, but also highly reusable. Commands in any MVC framework should do the same thing: input then output. In MyMVC, command classes spit out output in the term of events (MyMVCEvent). If they perform their jobs successfully, they dispatch "complete" event which also includes the result. If fails, they dispatch "error" event which also includes the error message. If the process is lengthy, they may dispatch "progress" event with progress information.

The ability of dispatching events also makes sequence command mentioned above possible. How does the front controller know when the first command finishes and then call the second command? Event.

Front controller can do "switching" work

Front controller is like a manager who assigns jobs (events) to workers (commands). In a real-life application, it's frequent that the controller needs to assign jobs depending on result of previous commands. In MyMVC it's easy to to write such switching codes:

new MyMVCEvent('E1', ...).broadcast();

Event has "who" and "what" properties

When an event is broadcasted, it's natural to inquire who broadcasts the event, and what function broadcasts the event in addition to the content of event. This is especially useful in MVC framework as many events are usually broadcasted (i.e. dispatched by a central agent), instead of dispatched. Let's say the front controller heard 'complete' event, he should know who completes the job and then can decide what to do next.


Download Source

Relation with Cairngorm

MyMVC was adopted from Cairngorm. It also incoporates many good features from real life applications (such as Employee Directory). The principles listed above makes it flexible and easy to build complex applications.

MyMVC based applications

  • Peaya Paper


Xu Cui (cuixiaofei AT

3/20/2009 Created

12/01/2009 modified