PureMVC Architects Lounge

Announcements and General Discussion => Architecture => Topic started by: kingcu on October 17, 2008, 01:42:38



Title: Multiple instances of the same mediator/proxy?
Post by: kingcu on October 17, 2008, 01:42:38
Greetings, everyone!

I just finished reading through the docs (hopefully I understood most of them) and started to really use puremvc in my app. There is a situation that the same view component will have multiple instances at the same time and thus multiple instances of the same mediator/proxy. To give you an concrete example, I have a Business explorer, where you can search for businesses and result will be displayed as a list; when user selects a business in the list, a new tab will opened to display the details of this business. So you could end up with multiple tabs open at the same time to display multiple business details. I will have the following classes:

BusinessView - view component that displays business detail
BusinessViewMediator - mediator for BusinessView
BusinessDetailProxy - proxy that loads/updates business detail

Here are the questions I can think of for now:
  • How should I name each of the instances when registering them?
  • When instantiating these instances, I am thinking to use a command that will create an instance of the mediator and then create the corresponding instance of the proxy, is this the right way to instantiate them?
  • To keep the link between corresponding mediator and proxy, do I need to store reference to each other in them?
  • When BusinessDetailProxy sends a notification, how do I make sure that only the corresponding BusinessViewMediator instance act upon it?

Thanks in advance.


Title: Re: Multiple instances of the same mediator/proxy?
Post by: Joel Hooks on October 17, 2008, 05:30:57
Each mediator or proxy needs a unique ID. One way to accomplish this is to pass a unique identifier in the mediator/proxy's constructor:

:
public static const NAME:String = "MyUniqueMediator";
private var aValueObject:MyVO;

public function MyUniqueMediator(viewComponent:Object, aValueObject:MyVO)
{
        this.aValueObject = aValueObject;
        super(this.getMediatorName, viewComponent)

}

override function getMediatorName():String
{
        return NAME+aValueObject.uniqueProperty;
}

now you can access it with :
:
var mediator:MyUniqueMediator = facade.retrieveMediator(MyUniqueMediator.NAME+aValueObject.uniqueProperty as MyUniqueMediator;
This works the same for proxies.


Title: Re: Multiple instances of the same mediator/proxy?
Post by: kingcu on October 17, 2008, 06:14:53
Thanks for the reply. I got the idea of unique id + NAME. What about the notification then? To my understanding, the current notification mechanism of puremvc is at "class" level, instead of instance level. Any good idea to make it work at instance level?


Title: Re: Multiple instances of the same mediator/proxy?
Post by: Joel Hooks on October 17, 2008, 06:27:30
Thanks for the reply. I got the idea of unique id + NAME. What about the notification then? To my understanding, the current notification mechanism of puremvc is at "class" level, instead of instance level. Any good idea to make it work at instance level?

:
case NotificationSender.IMPORTANT_NOTIFICATION:
if(notification.getBody().uniqueIdentifier == this.aValueObject.uniqueProperty)
{
     doStuff();
}

So notifications are declared at the class level, but you can check against your instance to see if it needs to react.


Title: Re: Multiple instances of the same mediator/proxy?
Post by: JJfutbol on October 21, 2008, 12:35:53
@kingcu

If I may suggest a better approach. The problem I've had with appending the name to the id is its just not flexible enough. Imagine a multiple instance of the same mediator and proxy for a dynamically created component. If I have an id with the name containing "mediator" and if I'm sending a notification from the proxy I will have to parse out "proxy" from the id and add "mediator" within the name so that way my mediator correctly responds to the notification. Way to much work.

I have written a post on the forums regarding this approach in detail. I've had to do a lot of dynamic stuff in my Flex projects and since I use PureMVC I've done this several times. I've had several of the same questions as you. Hope the post helps out. You can find it at: http://forums.puremvc.org/index.php?topic=596.0

Let me know if you have any questions.