1

I have an application in which I call a java method from the javascript side, like so: rpcProxy.myFunction(parameter); The function is of course defined in Java and it all works OK. But now, it has become necessary to have a callback function so that i can run some code only when myFunction has finished executing, on the javascript side that is. Now, I have never dealt with callback functions as such, the only ones I know are from jquery really, so I looked at a few javascript tutorials on callback functions - this seemed OK- and it makes almost sense, but the thing is that myFunction is of course executed in Java, so I can't figure out how to get a callback function in js executing it after that one has finished. Any idea?

On the java side:

@Override
  public void myFunction(String myParameter)
  {
//handling callback 
   com.vaadin.ui.JavaScript.getCurrent().addFunction("my.vaadin.project.fileUploader2", new JavaScriptFunction()
            {
                @Override
                public void call(JsonArray arguments)
                {
                    System.out.println("call() executed");
                    Notification.show("Received call");

                }
            });
  }
Community
  • 1
  • 1
antobbo
  • 255
  • 1
  • 4
  • 21
  • 2
    Did you check the [documentation](https://vaadin.com/docs/-/part/framework/advanced/advanced-javascript.html)? – Morfic Jul 29 '16 at 14:26
  • In fairness I read something else, not exactly that though, so I will have a good look and report back, cheers – antobbo Aug 01 '16 at 07:30
  • Right, function added on the java side, and I've updated the thread so you can see what I have done now. The documentation just says that >You only need to implement the call() method to handle calls from the >client-side JavaScript. So, now that I have the java side sorted, how do I handle the callback on the client side? There I have just `rpcProxy.myFunction(parameter);` – antobbo Aug 01 '16 at 10:07
  • are you sure that there is nothing like `rpcProxy.myFunction(parameter, callback);` That way callback function is executed in JS side. – Luis González Aug 01 '16 at 10:22
  • I was trying something like `rpcProxy.myFunction(parameter, function(){call("This msg!");});` but I guess my problem is that I don't know how to handle call backs on the js side. So you're saying that I have to pass the call back function as a parameter to myFunction? That means that I have to update its signature in the RPC interface as it's accepting only one parameter at the moment – antobbo Aug 01 '16 at 10:58
  • This approach won't work AFAIK, the Java cannot call the callback just like that. IMHO you will need to declare two explicit methods: one from client-to-server (a.k.a. `ServerRpc`) and one from server-to-client (you guessed it: `ClientRpc`). Passing parameters (e.g. a unique string value) from client to server and back allows your client code to know what previous call is being "callbacked". – geert3 Aug 01 '16 at 11:52
  • @geert3, that's already been done,the original method is defined in java and called from the js with an RPC call, I need to have a callback function that executes when that method has finish doing its thing. – antobbo Aug 01 '16 at 12:07
  • that's what I mean: you define a single clientRpc method that you use as generic callback method. By passing some unique value from client to server (in the serverRpc) the server passes that value back into the generic clientRpc method, so that the client knows for what serverRpc he is now receiving a "callback". – geert3 Aug 01 '16 at 12:54

0 Answers0