在 GWT 中验证多个异步方法

Validating multiple asyncronous methods in GWT

我正在查看我必须处理的代码。基本上我必须向按钮的侦听器添加验证。 该代码已经进行了多次验证。它们有点像级联。 按钮的侦听器调用一个 asyncCallBack 方法,如果一切正常,该方法的 onsuccess 部分将调用下一个,然后调用下一个,直到它到达末尾并转到下一页。我不喜欢这种方法,因为它有点乱。使用最佳实践的最佳方法是什么。

代码示例:

Button btnOK = new Button("Aceptar");
btnOK.addListener(Events.Select, new Listener<ButtonEvent>() {
public void handleEvent(ButtonEvent e) {
    myService.getInfo1(1, txt, "N",
        new AsyncCallback<List<InfoService>>() {
            public void onFailure(Throwable caught) {
            // goes back
                return
            }

            public void  onSuccess(
                List<Object> result) {
                    // do some validation with the result
                    validation2();
                }

        }
    }
}

public void validation2(){
    myService.getDireccionCanalesElectronicos(id, new AsyncCallback<MyResult>() {
        public void onSuccess(MyResult result) {
            // do some validation with the result
            validation3();
        }
        ...
    }
}

public void validation3(){
    myService.getDireccionCanalesElectronicos(id, new AsyncCallback<MyResult>() {
        public void onSuccess(MyResult result) {
            // do some validation with the result
            validation4();
        }
        ...
    }
}

有没有更好的方法,看起来很乱,很难理解。添加另一个验证很复杂。这似乎不是一个好习惯。

在调用所有验证方法的 servlet 中创建 1 个方法并在客户端中只执行一个调用?

public void validation()
{
   boolean ok = validation1();
  if (ok) ok = validation2();

  return validation;
}

使用mirco 服务有时很难处理。正如@Knarf 提到的,这是一条路要走。但有时您可能希望在客户端处理调用。另一个将使用这个微型框架:sema4g。它将帮助您解决问题。

解决方案可能如下所示:

首先创建 sem4g 命令:

private SeMa4gCommand createGetInfoCommand() {
  return new AsyncCommand() {
    // create callback
    MethodCallbackProxy<List<InfoService>> proxy = new MethodCallbackProxy<List<InfoService>>(this) {
      @Override
      protected void onProxyFailure(Method method,
                                    Throwable caught) {
        // Enter here the code, that will
        // be executed in case of failure
      }

      @Override
      protected void onProxySuccess(Method method,
                                    List<InfoService> response) {
        // Enter here the code, that will
        // be executed in case of success
      }
    };

    @Override
    public void execute() {
      // That's the place for the server call ...
      myService.getInfo1(1, txt, "N", proxy);
    }
  };
}

为您的所有通话执行此操作;

private SeMa4gCommand createCommandGetDireccionCanalesElectronicos() {
  return new AsyncCommand() {
    // create callback
    MethodCallbackProxy<MyResult> proxy = new MethodCallbackProxy<MyResult>(this) {
      @Override
      protected void onProxyFailure(Method method,
                                    Throwable caught) {
        // Enter here the code, that will
        // be executed in case of failure
      }

      @Override
      protected void onProxySuccess(Method method,
                                    List<MyResult>  response) {
        // Enter here the code, that will
        // be executed in case of success
      }
    };

    @Override
    public void execute() {
      // That's the place for the server call ...
      myService. getDireccionCanalesElectronicos(id, proxy);
    }
  };
}

为所有调用完成此操作后,创建一个 sema4g 上下文并运行它:

  try {
    SeMa4g.builder()
      .addInitCommand(new InitCommand() {
                            @Override
                            public void onStart() {
                              // Enter here your code, that
                              // should be executed when
                              // the context is started
                            })
      .addFinalCommand(new FinalCommand() {
                             @Override
                             public void onSuccess() {
                               // Enter here the code, that will
                               // be executed in case the context
                               // ended without error
                             }

                             @Override
                              public void onFailure() {
                                // Enter here the code, that will
                                // be executed in case the context
                                // ended with an error
                              })
      .add(createGetInfoCommand())
      .add(createCommandGetDireccionCanalesElectronicos())
      .build()
      .run();
  } catch (SeMa4gException e) {
    // Ups, something wrong with the context ...
  }

有关详细信息,请阅读文档。如果您有任何疑问,请随时提问:SeMa4g Gitter room.

希望对您有所帮助。