MVC & Robotlegs View Utility
Categories: MF Buzz
As a developer you often find yourself implementing the same basic functionality and logic over and over again. At its simplest, your application needs to present some graphical output, and react to user input. A more advanced application might need to fetch data from a third party service, store user data in a database, load assets, display dynamic content, share content etc. etc.
Almost regardless of the complexity, most applications need some sort of common base functionality. This common functionality could be:
- A solid structure so that your application tiers can communicate well.
- A storage manager which can manage the behavior and data of the application domain
- A set of views to display the graphical output in your application
- Sets of controllers to accept user input and instruct the view and the storage point to perform actions based on that input.
If you separate these concerns you get a design pattern known as the model-view-controller (MVC) design pattern. “The goal of MVC is, by decoupling models and views, to reduce the complexity in architectural design and to increase flexibility and maintainability of code.” (Wikipedia)
There are some great flash frameworks out there that helps you do just that. My personal favorite is Robotlegs which is a pure AS3 micro-architecture framework. It provides the glue in your application that helps you wire your object together. It also promotes loose coupling through the use of interfaces which makes your code highly testable.
Starting out with a solid framework as the base in your application you are up to a good start. But even with a solid framework as the application base you still need some standard behavior in typical flash projects. For example, you always need to add- and swap views. Furthermore, most of your views needs some sort behavior when they appear and disappear (eg. set some properties, animate in or out, release listeners for gc etc.). Your views might also be interested in knowing when other stuff is happening outside the scope of the view and perform some kind of action. The browser might be resized and your view needs to adjust position or other views are being added or removed from the application and your view should change state (eg. enable and disable menu items).
Wouldn’t it be great if that functionality was provided to you by default? Then all you need to do is override abstract methods to implement application specific behavior and call convenience methods to performing typical view related tasks?
I have made a view utility that aims to automize these typical view related behaviors. It is built on top of Robotlegs mvc+s framework and is using AS3Signals as the messaging tool.
Here is a list of the most important features:
- Easy to use. You need close to none Robotlegs experience to use it, see example files found at Github to get going.
- Provides abstract methods that are called at specific time during a view’s “life” and when framework events occur.
- Provides convenience methods for adding- swapping and removing views regardless of level/context the call is made from.
- A view controller for creating new views and add them to the mapped instance. If an instance of the mapped container/canvas isn’t present on stage, the controller creates a new and adds it to its mapped container.
- No need to map your views to a mediator, the default mediator is always mapped and added when a view is added. Saves time and the number of classes needed to get typical view functionality set up.
- You have access to default signals handlers in the mediator. Nice to have if you need the mediator to respond and do some action when for example the view starts or ends animation.
- You are not locked to the utility, you can still code your mediators, models, commands like you normally do. If you need to add more functionality to the mediator, just extend the utility’s mediator with your custom classes.
The utility can be found here. Check it out, play around and let me know what you think.
References:
- Model–view–controller – Wikipedia, the free encyclopedia
- Robotlegs homepage
