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 View Components in 1 Mediator  (Read 19741 times)
mores
Newbie
*
Posts: 5


View Profile Email
« on: August 05, 2009, 06:11:58 »

So far I have been creating 1 View Component to 1 Mediator.

Within the Mediator I add an addEventListener to that 1 specific View Component.

Is it possible to add an addEventListener to some other View Component ?

:
class UserViewMediator extends Mediator
{
        override public function onRegister():void
        {
            userView.addEventListener( UserView.LIST, onList);
           
            // How can I call something like this
            otherView.addEventListener( OtherView.SAVE, onSave );
        }
 
}
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: August 07, 2009, 08:11:36 »

Sure. Assuming that OtherView is a subcomponent of UserView with an id of otherView then based on your example:

:
class UserViewMediator extends Mediator
{
        override public function onRegister():void
        {
            userView.addEventListener( UserView.LIST, onList);
           
            // How can I call something like this
            otherView.addEventListener( OtherView.SAVE, onSave );
        }
 
}


private function get userView():UserView
{
        return viewComponent as UserView;
}

private function get otherView():OtherView
{
        return userView.otherView as OtherView;
}

Of course this is edging into the territory of breaking the encapsulation of UserView. Particularly if the OtherView instance ends up being several layers deeper in hierarchy and not a top-level property of the UserView. The farther you go down that road the worse an idea the single mediator becomes, as the mediator's knowledge and dependence on that view hierarchy makes it difficult to refactor the component without involving the mediator in the process.

However, the alternative of having to add an OtherViewMediator edges us into the territory of greater complexity.

As it stands above, this single mediator is a reasonable tradeoff.

-=Cliff>
Logged
mores
Newbie
*
Posts: 5


View Profile Email
« Reply #2 on: August 07, 2009, 02:00:57 »

Thanks for your reply, but the otherView was not going to be a subcomponent of userView.

I'll try to better define what I am trying to accomplish.

My app started from the Flex Slacker: http://puremvc.org/pages/demos/AS3/Demo_AS3_Flex_Slacker/

- The splash screen contains a logon screen.
- The first button "Gallery" is available to all
- If a regular user logs in, the next button "Editor" will be available.
- If a admin user logs in, buttons "Editor" and "Profile" will be available.

I am not really sure how best to accomplish this.

How could <mx:Application> Listen for notifications ?

 I was thinking about putting a new view "<view:HeaderDisplay>" inside of the "<mx:ApplicationControlBar>", but then I have multiple views.

What would be the best way to design this ?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: August 09, 2009, 08:06:27 »

I don't advise having a Mediator handle more than one view component if they are not parent and child with the Mediator connected to the parent. It just muddies the responsibilities and makes for less maintainable code.

There are lots of ways to accomplish what you're talking about. The ControlBarMediator could hear a notification about the login and set a VO on the controlbar where the buttons would bind to and disable or hide themselves based on the type of user.
 
-=Cliff>
Logged
Pages: [1]
Print