0

我的 Robotlegs 应用程序中的某些服务需要参数。我想知道哪种解决方案更好:

  1. 中介将参数传递给服务
  2. 服务从注入的模型中获取参数
4

1 回答 1

2

正如 Creynders 所建议的,这取决于这些变量的范围,如果它们是 const、模型或用户输入。

对我来说,ActionScript Developer's Guide to RobotLegs 是一个很好的资源:http: //books.google.ca/books/about/ActionScript_Developer_s_Guide_to_Robotl.html ?id=PFA2TWqZdSMC&redir_esc=y

这是我通常的工作流程:

  1. View 调度一个自定义事件并将参数传递给该事件。
  2. Mediator 监听事件并重新调度它。
  3. 上下文将事件映射到命令。
  4. 命令注入事件、任何必要的模型和服务。
  5. 命令调用服务,传递任何必要的参数。在下面的示例中,我将一个变量从 LoadLicenseEvent 和 ITokenModel 传递到服务调用。我使用 commandMap.detain() 和 commandMap.release() 使命令保持活动状态,直到服务调用完成。基类 ServiceModuleCommand 处理故障事件。

    public class LoadLicenseCommand extends ServiceModuleCommand
    {
        [Inject]
        public var event:LoadLicenseEvent;
    
        [Inject]
        public var service:ILicenseService;
    
        [Inject]
        public var tokenModel:ITokenModel;
    
        [Inject]
        public var licenseModel:ILicenseModel;
    
        public override function execute():void
        {
            commandMap.detain(this);
    
            var token:TokenVO = tokenModel.getToken();
    
            var asyncToken:AsyncToken = service.getLicense(token.Id, event.id);
            asyncToken.addResponder(new mx.rpc.Responder(resultHandler, faultHandler));
        }
    
        private function resultHandler(e:ResultEvent):void
        {
            var license:LicenseWebViewVO = e.result as LicenseWebViewVO;
            if (license)
            {
                licenseModel.license = license;
                dispatchToModules(new RunWidgetEvent(WidgetType.getWidgetId(WidgetType.LICENSE)));
            }
    
            commandMap.release(this);
        }
    
于 2012-09-07T15:58:20.140 回答