Say you have an application where you manage users. You provide a multi-tasking UI, using flexlib’s SuperTabNavigator to provide multiple tabs with each tab representing a user. Maybe its a form where they can edit that data or simply view it. You could expect to have at least the following classes:
- User (MXML component)
- UserVO
- UserProxy
- UserMediator
Now the problem becomes that you need to create multiple instances of these objects and register the proxies and mediators dynamically with PureMVC. Another problem is when one of these objects sends a notification that is specific to that instance it you need a way to ‘tag’ it. I’ve been using the notification’s type property for that. Cliff Hall and others have suggested the same.
I figured setting and defining a best practice for consistency would help others. Let me describe what I have been doing in my PureMVC projects to achieve this in a clean, productive manner.
First I need to allow the constructor of a mediator and a proxy to take a dynamic name. The constructor would take the NAME constant as a default but you would always override it. So I would setup a UserProxy like this:
/**
* Constructor.
*/
public function UserProxy(proxyName:String=NAME)
{
super(proxyName, new UserVO());
}
This way I can provide a unique name. The same applies to a Mediator. Next I would create an implicit getter to abstract the call to getMediatorName() or getProxyName().
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
//----------------------------------
// id
//----------------------------------
/**
* The proxy's unique id. This is a dynamically registered proxy.
*/
public function get id():String
{
return getProxyName();
}
The same code above applies to a Mediator. This provides for a consistent name and doesn’t have your proxy or mediator littered with getMediatorName() or getProxyName() calls whenever you are sending notifications.
Now for the actual creation of the objects I would have a command take care of that, this way its easily reusable and the all object registrations are in one place. Nice! As an example I would have a command named CreateUserEditorCommand and it would look this:
/**
* Create a new User editor with its own mediator and proxy and then
* we send a notification to add it to the set of tabs.
*/
override public function execute(note:INotification):void
{
var uid:String = UIDUtil.createUID();
var user:User = new User(); // this is our MXML component
user.label = "Cliff Hall";
user.id = uid;
facade.registerProxy( new UserProxy( uid ) );
facade.registerMediator( new UserMediator( uid, user ) );
sendNotification(ApplicationFacade.ADD_EDITOR, user);
}
The User instance is our MXML component (our editor tab) and if you look carefully I’m setting its id property to a unique identifier. I use the UIDUtil class (part of the Flex framework) to make sure I always have a unique id. The reason I set it on the MXML component is this way I have access to it there. This might not be needed.
I’m not sure if ‘id’ is the best name to be used here and hoping to get suggestions from all of you. I’ve talked about this approach with my fellow employees and they like it.
I had a suggestion from our Flex team leader to use the ‘name’ property instead of ‘id’ on the MXML component especially since Flex uses ‘id’ internally. I figured if I’m setting it with something unique though it shouldn’t be a problem. I have been considering instead using ‘name’ as the implicit getter on the mediator and proxy and on the MXML component.
Thoughts?
If this generates interest I’ll share how I register sub proxies and mediators as well as a command to clean up these dynamically registered actors.