0

Execute JavaScript and get the result synchronous from server

asked 2013-09-29 09:50:18 +0800

jasonhoo gravatar image jasonhoo
104 4

I want to implements: 1, Click one button to do something in server 2, Call JavaScript to do something in Client with the server's data 3, Do something in server with the result of Client. All of these must be synchronous like Messagebox.show, Clients.evalJavaScript can execute JavaScript but it isn't synchronous and cann't get the result.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-11-02 09:09:26 +0800

jasonhoo gravatar image jasonhoo
104 4

updated 2013-12-13 04:08:24 +0800

I implements it as follow(PS: ctx is a vary in session):

public Object evalJavaScriptSynchronized(String script)
{
   script = "_data=" + script + "zAu.send(new zk.Event(null,\"onResult\",{result:__data},{toServer:true}));";  // Use zAu.send to return the result.
   Clients.evalJavaScript(script);
   Clients.showBusy("Wait for JavaScript's result...");
   try
   {
      Executions.wait(ctx.mutex); // Wait for JavaScript executed synchronized.
   }
   catch(Exception e)
   {}
   Clients.clearBusy();
   return ctx.result;  // Return the result. 
}

public class ResultAuService
    implements AuService
{
  @Override
  public boolean service(AuRequest request, boolean everError)
  {
    String command = request.getCommand();
    if ("onResult".equals(command))
    {
      ctx.jsResult = request.getData().get("result");
      Executions.notifyAll(ctx.mutex);
      return true;
    }
    return false;
  }
}
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-09-29 09:50:18 +0800

Seen: 39 times

Last updated: Dec 13 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More