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: From where do you get the component for a Mediator to mediate?  (Read 10948 times)
Karl Macklin


Email
« on: June 09, 2009, 08:36:09 »

When first learning PureMVC I had it totally wrong:

I was passing the stage to ALL my mediators. For example I had a MouseMediator that handled the logic in my game when I held down the space button and it was supposed to change the cursor into a bomb. That mediator handled some child view components (the custom cursor and some particle effects).

Now after reading up more on the framework and viewing a lot of demos, I see that often times in a startup command it goes something like this:
Register a mediator while creating a new instance of said mediator, while passing the view component it should mediate as the argument.

For example the Employee Admin demo does this and uses components that are defined in the main application mxml. By defined, in this case, I mean it is instantiated there. Of course the component itself has its own file.
Example:
facade.registerMediator( new UserFormMediator( app.userForm ) );

What I fail to grasp is how this is handled if you don't use Flex.
Let me describe my situation to explain my issue:

I use FlashDevelop, currently making a flash game. I don't use the Flash IDE whatsoever, so there's no components being exported as classes on compilation. Even if there was, I'm not sure it would make a difference.
In my current messy code I have all mediators get the stage as the argument when they are created. The mediators themselves then add their mediated view components in their constructors by instantiating them.
Far from how it's done in the Demo examples.

I just don't know WHERE they otherwise would be instantiated? In and by the StartupCommand? In my Main.as that comes before the Facade?
Logged
Jason MacDonald
Sr. Member
****
Posts: 243


View Profile Email
« Reply #1 on: June 09, 2009, 10:40:05 »

For AS3 only projects where the component to mediate is not yet on the stage, you can create it inside of the mediator itself, or inside of another command/mediator that is registering the mediator.

Also, try to do any creation inside of the mediators onRegister() method and not the constructor to ensure the mediator is fully registered with the PMVC apperatus.

Examples:

Register and create component inside mediator
:
// when a mediator is created and you want the mediator itself to create the component
...
override public function onRegister():void
{
    viewComponent = new ComponentToMediate();
    sendNotification(ADD_TO_STAGE, viewComponent, 'type');

}

...

Inside command
:
// when you want a command to handle it (inside of a command)
override public function execute(note:INotification):void
{
    ....

    facacde.registerMediator(new compMediator(new UIComponent()));

}

// then inside said mediator
override public function onRegister():void
{
    // mediator registered alert we need to add this component to stage
    sendNotification(ADD_TO_STAGE, viewComponent, 'type');

}

Have another mediator that is the parent of the component handle it based on some notification
@See the HelloFlash demo for an example of this
:

handleNotification(note:INotification):void {

    switch(note.noteName())
    {
         case 'createSomeChild':
              var comp:UIComponent = new UIComponent();
              facade.egisterMediator(new CompMediator(comp));
              viewComponent.addChild(comp);
              break;
    }
}
Logged
Alci
Newbie
*
Posts: 5


View Profile Email
« Reply #2 on: January 18, 2010, 09:37:20 »

If you create the component in the onRegister(), then how do you write the get function in the mediator?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: January 19, 2010, 08:41:41 »

Same as any other Mediator:

:
private function get myCustomComponent():MyCustomComponent
{
    return viewComponent as MyCustomComponent;
}
Logged
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« Reply #4 on: February 16, 2010, 10:57:13 »

Just curious, but what is the thinking behind instantiating in onRegister() vs in the Mediator's constructor?  The only thing I can think of is that it seems correct.  Technically, the Mediator doesn't do anything until it is registered so by extension it is wasteful and/or presumptuous to instantiate a potentially heavy view component in the constructor until we know for sure the Mediator is registered... but at the same time, I'm being told this is too esoteric and since in our project the constructor and onRegister() are effectively simultaneous we should just use the constructor (keep-it-simple-stupid is a common rebuttal).  Is there any other less esoteric reason to instantiate in onRegister()?

Sorry this is kind of a nit-picky question.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #5 on: February 17, 2010, 10:16:36 »

The reason for holding off until onRegister really is to keep the Mediator from starting any conversations it's not able to participate in.

If, while the Mediator is not yet registered any events come out of the component that trigger listeners in the Mediator which would in turn cause the Mediator to send a notification, the Mediator could receive any potential response notifications.

-=Cliff>
Logged
Pages: [1]
Print