在flex puremvc框架中注册代理和调解器的最佳位置是什么?

时间:2009-11-10 09:20:45

标签: design-patterns flex3 puremvc

我使用puremvc框架开发基于flex的项目。我的问题是什么是延迟注册代理类和中介类的最佳方法? 目前正在启动命令我正在注册启动中介。

我的代码有:

  1. ApplicationFacade.as
  2. StartupCommand.as
  3. StartupMediator.as
  4. LoginCommand.as
  5. LoginMediator.as
  6. LoginProxy.as
  7. LoginView.as
  8. ApplicationFacade.as 中,我向 StartupCommand 发送通知。 StartupCommand 注册 StartupMediator

    现在我的问题:

    1. 何时何地注册 LoginMediator
    2. 何时何地注册 LoginProxy
    3. 何时向其发送通知 LoginCommand
    4. 如果我们注册 LoginMediator LoginCommand 中的 LoginProxy 就像         

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

      现在,如果我发送通知 LoginCommand 然后多次 创造了多重的机会 LoginProxy LoginMediator 。又怎样 避免它?

1 个答案:

答案 0 :(得分:1)

  1. 在我的startUpCommand中,我注册了当前在舞台上有视图的所有调解器。我等待注册任何其他观点和调解员,直到他们需要。

  2. 我在startUpCommand中注册了几乎所有Proxies,因此他们可以从flashVars注册并从服务器加载数据。至于您的LoginProxy,我会在您的StartUpCommand中创建它以帮助您入门。应用程序增长后,您可以将其移动到设置登录Mediator的命令。

  3. 我建议将您的代码放在switch语句中,以确保您在正确的通知上执行代码并删除命令。

      

    覆盖公共函数execute(通知:INotification):void {
          switch(notification.getName()){
          案例AppFacade.START_UP:
        //删除启动命令
      facade.removeCommand(notification.getName());
      打破;
      }
      }

    您还可以在外观上使用hasProxy方法,以确保您不注册两个LoginProxies。

      

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