PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: yancaoshi on October 14, 2008, 09:20:18



Title: How to manage multi-view in one mediator?
Post by: yancaoshi on October 14, 2008, 09:20:18
Hi,

According to best pratice, a Mediator can be shared by several views. When initializing a mediator, we should pass that view component to constructor.
Now, I want to manage two relevant views in a mediator, one of which is to show the user list and the other of which is to edit the single entry. Since they are two different view components, how can I initialize the mediator?

public function Mediator( mediatorName:String=null, viewComponent:Object=null )

Thanks a lot.

yancaoshi


Title: Re: How to manage multi-view in one mediator?
Post by: Jason MacDonald on October 14, 2008, 10:40:22
The name of the mediator is all you need to be different.
:
facade.register(new Mediator('CustomName1', viewComponent);
facade.register(new Mediator('CustomName2', viewComponent);

A better way is to have your components return a unique name and use that in the constructor of the mediator.
:
public function Mediator(viewComponent:Object=null ) {
     super(viewComponent.getUniqueName(), viewComponent);
}


Title: Re: How to manage multi-view in one mediator?
Post by: puremvc on October 14, 2008, 06:44:58
A mediator has a viewComponent property that is an Object. That can be anything. So if you want one mediator to manage 2 view components the first idea is to mediate the container that contains both objects. If this is not possible or reasonable, then simply pass an array with the 2 view components as elements. Then make typed getters in the mediator that return element 0 or element 1 of the array cast to the appropriate type.

You can make the constuctor of this mediator take 2 typed arguments so that you are sure it is created with the right types. Then pass the array to super, as the view component argument. Of course this mediator like any other needs a name, so give it one that reflects that it mediates both components.

Since you used typed arguments for the constructor you can be sure the casts of the array elements to the proper types will not fail.

-=Cliff>


Title: Re: How to manage multi-view in one mediator?
Post by: Jason MacDonald on October 14, 2008, 07:29:11
Oops, looks like I misread the question. I thought you wanted new instances of one mediator to handle each component. My bad.