You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-javascript.asciidoc 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ---
  2. title: JavaScript Interaction
  3. order: 14
  4. layout: page
  5. ---
  6. [[advanced.javascript]]
  7. = JavaScript Interaction
  8. Vaadin supports two-direction JavaScript calls from and to the server-side. This
  9. allows interfacing with JavaScript code without writing client-side integration
  10. code.
  11. [[advanced.javascript.calling]]
  12. == Calling JavaScript
  13. You can make JavaScript calls from the server-side with the
  14. [methodname]#execute()# method in the [classname]#JavaScript# class. You can get
  15. a [classname]#JavaScript# instance from the current [classname]#Page# object
  16. with [methodname]#getJavaScript()#.//TODO Check that the API is
  17. so.
  18. [source, java]
  19. ----
  20. // Execute JavaScript in the currently processed page
  21. Page.getCurrent().getJavaScript().execute("alert('Hello')");
  22. ----
  23. The [classname]#JavaScript# class itself has a static shorthand method
  24. [methodname]#getCurrent()# to get the instance for the currently processed page.
  25. [source, java]
  26. ----
  27. // Shorthand
  28. JavaScript.getCurrent().execute("alert('Hello')");
  29. ----
  30. The JavaScript is executed after the server request that is currently processed
  31. returns. If multiple JavaScript calls are made during the processing of the
  32. request, they are all executed sequentially after the request is done. Hence,
  33. the JavaScript execution does not pause the execution of the server-side
  34. application and you can not return values from the JavaScript.
  35. [[advanced.javascript.callback]]
  36. == Handling JavaScript Function Callbacks
  37. You can make calls with JavaScript from the client-side to the server-side. This
  38. requires that you register JavaScript call-back methods from the server-side.
  39. You need to implement and register a [classname]#JavaScriptFunction# with
  40. [methodname]#addFunction()# in the current [classname]#JavaScript# object. A
  41. function requires a name, with an optional package path, which are given to the
  42. [methodname]#addFunction()#. You only need to implement the [methodname]#call()#
  43. method to handle calls from the client-side JavaScript.
  44. [source, java]
  45. ----
  46. JavaScript.getCurrent().addFunction("com.example.foo.myfunc",
  47. new JavaScriptFunction() {
  48. @Override
  49. public void call(JsonArray arguments) {
  50. Notification.show("Received call");
  51. }
  52. });
  53. Link link = new Link("Send Message", new ExternalResource(
  54. "javascript:com.example.foo.myfunc()"));
  55. ----
  56. Parameters passed to the JavaScript method on the client-side are provided in a
  57. [classname]#JSONArray# passed to the [methodname]#call()# method. The parameter
  58. values can be acquired with the [methodname]#get()# method by the index of the
  59. parameter, or any of the type-casting getters. The getter must match the type of
  60. the passed parameter, or an exception is thrown.
  61. [source, java]
  62. ----
  63. JavaScript.getCurrent().addFunction("com.example.foo.myfunc",
  64. new JavaScriptFunction() {
  65. @Override
  66. public void call(JsonArray arguments) {
  67. try {
  68. String message = arguments.getString(0);
  69. int value = arguments.getInt(1);
  70. Notification.show("Message: " + message +
  71. ", value: " + value);
  72. } catch (Exception e) {
  73. Notification.show("Error: " + e.getMessage());
  74. }
  75. }
  76. });
  77. Link link = new Link("Send Message", new ExternalResource(
  78. "javascript:com.example.foo.myfunc(prompt('Message'), 42)"));
  79. ----
  80. The function callback mechanism is the same as the RPC mechanism used with
  81. JavaScript component integration, as described in
  82. <<dummy/../../../framework/gwt/gwt-javascript#gwt.javascript.rpc,"RPC from
  83. JavaScript to Server-Side">>.