Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Multiple instances of the same mediator/proxy?  (Read 7977 times)
kingcu
Newbie
*
Posts: 2


View Profile Email
« 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.
Logged
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #1 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.
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
kingcu
Newbie
*
Posts: 2


View Profile Email
« Reply #2 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?
Logged
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #3 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.
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
JJfutbol
Sr. Member
****
Posts: 53


View Profile WWW Email
« Reply #4 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.
Logged
Pages: [1]
Print