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: RetrieveMediator for multiple instances  (Read 3807 times)
badoumba
Newbie
*
Posts: 8


View Profile Email
« on: October 18, 2011, 11:49:35 »

Hi guys

I read almost all the threads concerning the use of multi instances of a same Mediator. There is one thing that I didn't find: how to get back one of these specific mediator? Here is my case:

Architecture (simplified, easier than refectoring some example code):
*MyComponent.as is a component that I need to place 2 instances on my application. It has 2 states normal, and changed. Initial state is normal.
*MySubComponent.as is dynamically created and nested into each MyComponent. It has 1 button.

What I need to achieve:
When I press one of the 2 buttons from MySubComponent objects that are on my stage, the state of the corresponding parent (MyComponent) has to change from normal to changed.

What I've done:
* My ApplicationMediator creates 2 instances of a same MyComponent component + registers the 2 instances of MyComponentMediator using the myComponent.id property to make its name unique.
* Each MyComponent creates its own MySubComponent + registers its MySubComponentMediator.
* When I press the button of any MySubComponent, I send an Event to MySubComponentMediator.
* Here, I need to retrieve the MyComponentMediator associated to the MyComponent parent (and only this one!)in order to alter its state....here is my problem!

Thanks for your input!


« Last Edit: October 18, 2011, 11:51:32 by badoumba » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: October 19, 2011, 07:36:49 »

Here, I need to retrieve the MyComponentMediator associated to the MyComponent parent (and only this one!)in order to alter its state....here is my problem!
You don't need to retrieve the MyComponentMediator, you just need to send a notification it is interested in. Yes, both instances of MyComponentMediator will respond to the note, so you pass something in the type property of the notification that will allow the two instances of MyComponentMediator to discriminate and only act on notes meant for the right instance. This could be a bit of information shared between MyComponent and MySubComponent, for example.  MyComponentMediator could look at the type property and if it is equal to some property on its view component, then it acts on the note.

Here is my question to you: is there a good reason for you to build this big mediation loop? Why doesn't MyComponent listen to MySubComponent for the same event, and change its state in the handler? Why go the long way around the barn?

Now there may be a perfectly good reason. Perhaps once the Mediator is in receipt of the event, it needs to collaborate with a Command to perform some business logic or maybe it needs to fetch some data from a Proxy and pass it back to the view component. If this is the case then consider the above solution and carry on.

But if the whole loop out through mediator-land is just an exercise in trying to apply the framework, then take the shortcut and don't mediate this collaboration. View components should be able to encapsulate their own behavior for the most part and tying a simple state change to a button press certainly falls within that realm. Only when an interaction leads to some business logic interpretation, model change, or distant and awkward to achieve view component change does mediation really make sense.

-=Cliff>
Logged
badoumba
Newbie
*
Posts: 8


View Profile Email
« Reply #2 on: October 19, 2011, 11:54:17 »

You don't need to retrieve the MyComponentMediator, you just need to send a notification it is interested in. Yes, both instances of MyComponentMediator will respond to the note, so you pass something in the type property of the notification that will allow the two instances of MyComponentMediator to discriminate and only act on notes meant for the right instance. This could be a bit of information shared between MyComponent and MySubComponent, for example.  MyComponentMediator could look at the type property and if it is equal to some property on its view component, then it acts on the note.

I get the point! But this implies a kind of hard coupling between MyComponent and MySubComponent. But I imagine that we don't have any other choice that indicating which Mediator we need at one time or another anyway.

Here is my question to you: is there a good reason for you to build this big mediation loop? Why doesn't MyComponent listen to MySubComponent for the same event, and change its state in the handler? Why go the long way around the barn?

The program I am currently coding is actually a test required by a company to test my knowledges in framework usage. So my interest here was to code it as lose as possible! I admit that this kind of shortcut that you mention is probably smarter in some case like that where components are nested and no command or data exchange is required ::) Your explanations can't be more clear!

Thanks for your post.... number 2581!  :P

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: October 19, 2011, 12:25:59 »

But this implies a kind of hard coupling between MyComponent and MySubComponent.
not really, only that they both share a common piece of information, and that piece of information uniquifies the pair.
Logged
badoumba
Newbie
*
Posts: 8


View Profile Email
« Reply #4 on: October 19, 2011, 12:42:04 »

Ok. I could for instance code some static const variables in MyComponent and refer to these in MySubComponent as identifier.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #5 on: October 19, 2011, 12:59:54 »

I could for instance code some static const variables in MyComponent and refer to these in MySubComponent as identifier.

Well that actually would make a hard coupling between MyComponent and MySubComponent, since the latter would have to reference the former for the constant. Instead put them in a separate constants class.
Logged
badoumba
Newbie
*
Posts: 8


View Profile Email
« Reply #6 on: October 19, 2011, 01:07:51 »

Right! Still have a lot to learn  :-\
Logged
Pages: [1]
Print