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 :
- ApplicationFacade.as
- StartupCommand.as
- StartupMediator.as
- LoginCommand.as
- LoginMediator.as
- 开发者_StackOverflow社区LoginProxy.as
- LoginView.as
In ApplicationFacade.as I am sending notification to StartupCommand. StartupCommand registers StartupMediator.
Now my question:
- When and where to register LoginMediator ?
- When and where to register LoginProxy ?
- When to send notification to LoginCommand ?
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 ?
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.
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 ...
}
精彩评论