views:

265

answers:

1

I am using puremvc framework for developing flex based project. My question is related to what is best way to delayed registering proxy class and mediator class ? Currently on startup command I am registering startup mediator.

My code has :

  1. ApplicationFacade.as
  2. StartupCommand.as
  3. StartupMediator.as
  4. LoginCommand.as
  5. LoginMediator.as
  6. LoginProxy.as
  7. LoginView.as

In ApplicationFacade.as I am sending notification to StartupCommand. StartupCommand registers StartupMediator.

Now my question:

  1. When and where to register LoginMediator ?
  2. When and where to register LoginProxy ?
  3. When to send notification to LoginCommand ?
  4. If we registers LoginMediator and LoginProxy in LoginCommand like

         public class LoginCommand extends SimpleCommand
    implements ICommand {
        override public function execute(notification:INotification):void
        {
            facade.registerProxy( new LoginProxy() );
         facade.registerMediator( new LoginMediator( app ) );
        } } 

    Now if I am sending notification for LoginCommand multiple times then it creates multiple instace of LoginProxy and LoginMediator. So how to avoid it ?

+1  A: 
  1. In my startUpCommand I register all mediators that currently have views on stage. I wait to register any other views and mediators until they are needed.

  2. I register almost all Proxies in my startUpCommand so they can register from flashVars and load data from the server. As for your LoginProxy I'd create it in your StartUpCommand to get you started. Once your application grows you can move it to a command that sets up your login Mediator.

I'd suggest putting your code in a switch statement to ensure your executing code on the correct notification and removing the command.

override public function execute(notification:INotification):void {
switch(notification.getName()) {
case AppFacade.START_UP:
// REMOVE START UP COMMAND
facade.removeCommand(notification.getName() );
break;
}
}

You can also use the hasProxy method on the facade to ensure you don't register two LoginProxies.

if(facade.hasProxy(LoginProxy.NAME)) {
facade.registerProxy ...
}

jspooner
How to prevent mediator register multiple times and creating multiple instance of same mediator ?
Silent Warrior
You can prevent multiple mediators with the same method from above but use facade.hasMediator() method vs hasProxy.It would look something like this.if(facade.hasMediator(LoginMediator.NAME)){ facade.registerMediator(new LoginMediator(... bla bla...));} else{trace("Mediator exists. Probably should remove this command now");}
jspooner