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.

UsingRPCFromJavaScript.asciidoc 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ---
  2. title: Using RPC From JavaScript
  3. order: 42
  4. layout: page
  5. ---
  6. [[using-rpc-from-javascript]]
  7. = Using RPC from JavaScript
  8. This tutorial continues where
  9. <<IntegratingAJavaScriptComponent.asciidoc#,"Integrating a JavaScript
  10. component">> ended. We will now add RPC functionality to the JavaScript
  11. Flot component. RPC can be used in the same way as with ordinary GWT
  12. components.
  13. We will add RPC from the client to the server when the user clicks a
  14. data point in the graph and RPC from server to client for highlighting a
  15. data point in the graph. For each of these, we define an RPC interface.
  16. Each interface has one method that takes a data series index and the
  17. index of a point in that series. As with the shared state, the GWT code
  18. doesn't need to know about these interfaces and it's thus not required
  19. to put them in the widgetset's client package and to recompile the
  20. widgetset after making changes.
  21. [source,java]
  22. ....
  23. public interface FlotClickRpc extends ServerRpc {
  24. public void onPlotClick(int seriesIndex, int dataIndex);
  25. }
  26. public interface FlotHighlightRpc extends ClientRpc {
  27. public void highlight(int seriesIndex, int dataIndex);
  28. }
  29. ....
  30. The server side code for this looks the same as if the client-side
  31. connector was implemented using GWT. An RPC implementation is registered
  32. in the constructor.
  33. [source,java]
  34. ....
  35. public Flot() {
  36. registerRpc(new FlotClickRpc() {
  37. public void onPlotClick(int seriesIndex, int dataIndex) {
  38. Notification.show("Clicked on [" + seriesIndex + ", "
  39. + dataIndex + "]");
  40. }
  41. });
  42. }
  43. ....
  44. Highlighting is implemented by getting an RPC proxy object and invoking
  45. the method.
  46. [source,java]
  47. ....
  48. public void highlight(int seriesIndex, int dataIndex) {
  49. getRpcProxy(FlotHighlightRpc.class).highlight(seriesIndex, dataIndex);
  50. }
  51. ....
  52. The JavaScript connector uses similar functions from the connector
  53. wrapper: `this.getRpcProxy()` for getting an object with functions that
  54. will call the server-side counterpart and `this.registerRpc()` for
  55. registering an object with functions that will be called from the
  56. server. Because of the dynamic nature of JavaScript, you don't need to
  57. use the interface names if you don't want to - all methods from all RPC
  58. interfaces registered for the connector on the server will be available
  59. in the RPC proxy object and any RPC method invoked from the server will
  60. be called if present in the RPC object you registered. If a connector
  61. uses multiple RPC interfaces that define methods with conflicting names,
  62. you can still use the interface names to distinguish between interfaces.
  63. We need to make some small adjustments to the connector JavaScript to
  64. make it work with the way Flot processes events. Because a new Flot
  65. object is created each time the `onStateChange` function is called, we
  66. need to store a reference to the current object that we can use for
  67. applying the highlight. We also need to pass a third parameter to
  68. `$.plot` to make the graph area clickable. Aside from those changes, we
  69. just call the function on the RPC proxy in a click listener and register
  70. an RPC implementation with a function that highlights a point.
  71. [source,javascript]
  72. ....
  73. window.com_example_Flot = function() {
  74. var element = $(this.getElement());
  75. var rpcProxy = this.getRpcProxy();
  76. var flot;
  77. this.onStateChange = function() {
  78. flot = $.plot(element, this.getState().series, {grid: {clickable: true}});
  79. }
  80. element.bind('plotclick', function(event, point, item) {
  81. if (item) {
  82. rpcProxy.onPlotClick(item.seriesIndex, item.dataIndex);
  83. }
  84. });
  85. this.registerRpc({
  86. highlight: function(seriesIndex, dataIndex) {
  87. if (flot) {
  88. flot.highlight(seriesIndex, dataIndex);
  89. }
  90. }
  91. });
  92. }
  93. ....
  94. When the normal Vaadin RPC is used with JavaScript connectors, you can
  95. use the same server-side code that you would use with a GWT connector
  96. and the client-side code uses the same concepts as for GWT connectors,
  97. just translated to fit into the world of JavaScript.