PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: jgervin on March 04, 2010, 09:18:10



Title: Registering new mediators (duplicate?)
Post by: jgervin on March 04, 2010, 09:18:10
If I create a mediator via registerMediator or create a proxy with registerProxy and I call the same function twice will I get two of each or will it grab both mediator and proxy that already exist?

TIA, J


Title: Re: Registering new mediators (duplicate?)
Post by: jpwrunyan on March 04, 2010, 10:17:06
Ok, your question seems to contain a false assumption, but I will try to answer what I *think* you actually wanted to ask.

Here's the relevant code (from View):
:
public function registerMediator( mediator:IMediator ) : void
{
// do not allow re-registration (you must to removeMediator fist)
if ( mediatorMap[ mediator.getMediatorName() ] != null ) return;

So no you will not register two Mediators (or Proxies) to the same name if you call a register function twice with different instances that share the same name. 

eg:
:
var medA = new MediatorA("Id");
facade.registerMediator(medA);
var medB = new MediatorB("Id"); //same Mediator name!
facade.registerMediator(medB); //ignored, "Id" already taken

var med = facade.retrieveMediator("Id"); //returns same instance of MediatorA referenced by "medA"
The first one gets mapped and the second is ignored.  But also notice you don't grab anything with this function.  That seems to be your false assumption.  You can only grab with retrieve which only returns one Mediator (or Proxy for retrieveProxy()) and it will always be the first and only instance registered.


Title: Re: Registering new mediators (duplicate?)
Post by: puremvc on March 06, 2010, 03:34:23
If I create a mediator via registerMediator or create a proxy with registerProxy and I call the same function twice will I get two of each or will it grab both mediator and proxy that already exist?
Just to be clear, registerMediator and registerProxy don't actually create Mediators or Proxies they just register them. The idiom often used is that the construction of the Mediator or Proxy is nested within the call to the associated registration method.

But regardless, no, the register methods will not register overtop of an already registered Mediator or Proxy with the same name. You must unregister the previous instance first.

In case you are actually wanting to have two instances registered at the same time, you must give them different names.

Since most Mediators or Proxies only ever need a single instance, it is common to do this:
:
public class UserProxy extends Proxy
{
public static const NAME:String = 'UserProxy';
public function UserProxy( user:User )
{
super ( NAME, user );
}
}

But if the above Proxy needed to be registered separately for each user, you'd need to make the name unique, which you might do like this:

:
public class UserProxy extends Proxy
{
public static const NAME:String = 'UserProxy';
public function UserProxy( user:User )
{
super ( NAME+"/"+user.id, user );
}
}

-=Cliff>