我使用puremvc框架开发基于flex的项目。我的问题是什么是延迟注册代理类和中介类的最佳方法? 目前正在启动命令我正在注册启动中介。
我的代码有:
在 ApplicationFacade.as 中,我向 StartupCommand 发送通知。 StartupCommand 注册 StartupMediator 。
现在我的问题:
如果我们注册 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 。又怎样 避免它?
答案 0 :(得分:1)
在我的startUpCommand中,我注册了当前在舞台上有视图的所有调解器。我等待注册任何其他观点和调解员,直到他们需要。
我在startUpCommand中注册了几乎所有Proxies,因此他们可以从flashVars注册并从服务器加载数据。至于您的LoginProxy,我会在您的StartUpCommand中创建它以帮助您入门。应用程序增长后,您可以将其移动到设置登录Mediator的命令。
我建议将您的代码放在switch语句中,以确保您在正确的通知上执行代码并删除命令。
覆盖公共函数execute(通知:INotification):void {
switch(notification.getName()){
案例AppFacade.START_UP:
//删除启动命令
facade.removeCommand(notification.getName());
打破;
}
}
您还可以在外观上使用hasProxy方法,以确保您不注册两个LoginProxies。
if(facade.hasProxy(LoginProxy.NAME)){
facade.registerProxy ...
}