MyMVC, Adobe AIR (Flex) application framework
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):
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:
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:
And then broadcast the 'REGISTER' event 5 times with different data. Or, you can do:
When you broadcast 'PARALLEL_REGISTER' event, the event.data 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.
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:
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.
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
Xu Cui (cuixiaofei AT gmail.com)