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.

SimplifiedRPCusingJavaScript.asciidoc 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ---
  2. title: Simplified RPC using JavaScript
  3. order: 19
  4. layout: page
  5. ---
  6. [[simplified-rpc-using-javascript]]
  7. = Simplified RPC using JavaScript
  8. This tutorial continues where
  9. <<IntegratingAJavaScriptComponent#integrating-a-javascript-component,
  10. Integrating a JavaScript component>> ended. We will now add RPC
  11. functionality to the JavaScript Flot component. RPC can be used in the
  12. same way as with ordinary GWT components as described in
  13. <<IntegratingAJavaScriptComponent#integrating-a-javascript-component,
  14. Using RPC from JavaScript>>. This tutorial describes a simplified way that is
  15. based on the same concepts as in
  16. <<ExposingServerSideAPIToJavaScript#exposing-server-side-api-to-javascript,
  17. Exposing server side API to JavaScript>>. This way of doing RPC is less
  18. rigorous and is intended for simple cases and for developers appreciating
  19. the dynamic nature of JavaScript.
  20. The simplified way is based on single callback functions instead of
  21. interfaces containing multiple methods. We will invoke a server-side
  22. callback when the user clicks a data point in the graph and a
  23. client-side callback for highlighting a data point in the graph. Each
  24. callback takes a data series index and the index of a point in that
  25. series.
  26. In the constructor, we register the callback that will be called from
  27. the client-side when a data point is clicked.
  28. [source,java]
  29. ....
  30. public Flot() {
  31. addFunction("onPlotClick", new JavaScriptFunction() {
  32. public void call(JsonArray arguments) throws JSONException {
  33. int seriesIndex = arguments.getInt(0);
  34. int dataIndex = arguments.getInt(1);
  35. Notification.show("Clicked on [" + seriesIndex + ", "
  36. + dataIndex + "]");
  37. }
  38. });
  39. }
  40. ....
  41. Highlighting is implemented by invoking the client-side callback
  42. function by name and passing the appropriate arguments.
  43. [source,java]
  44. ....
  45. public void highlight(int seriesIndex, int dataIndex) {
  46. callFunction("highlight", seriesIndex, dataIndex);
  47. }
  48. ....
  49. The simplified RPC mechanism is based on JavaScript functions attached
  50. directly to the connector wrapper object. Callbacks registered using the
  51. server-side `registerCallback` method will be made available as a
  52. similarly named function on the connector wrapper and functions in the
  53. connector wrapper object matching the name used in a server-side
  54. `callFunction` will be called. Because of the dynamic nature of
  55. JavaScript, it's the developer's responsibility to avoid naming
  56. conflicts.
  57. We need to make some small adjustments to the connector JavaScript to
  58. make it work with the way Flot processes events. Because a new Flot
  59. object is created each time the onStateChange function is called, we
  60. need to store a reference to the current object that we can use for
  61. applying the highlight. We also need to pass a third parameter to
  62. `$.plot` to make the graph area clickable. We are finally storing a
  63. reference to `this` in the `self` variable because `this` will point to
  64. a different object inside the click event handler. Aside from those
  65. changes, we just call the callback in a click listener and add our own
  66. callback function for highlighting a point.
  67. [source,javascript]
  68. ....
  69. window.com_example_Flot = function() {
  70. var element = $(this.getElement());
  71. var self = this;
  72. var flot;
  73. this.onStateChange = function() {
  74. flot = $.plot(element, this.getState().series, {grid: {clickable: true}});
  75. }
  76. element.bind('plotclick', function(event, point, item) {
  77. if (item) {
  78. self.onPlotClick(item.seriesIndex, item.dataIndex);
  79. }
  80. });
  81. this.highlight = function(seriesIndex, dataIndex) {
  82. if (flot) {
  83. flot.highlight(seriesIndex, dataIndex);
  84. }
  85. };
  86. }
  87. ....
  88. When the simplified RPC functionality designed for JavaScript
  89. connectors, there's no need to define RPC interfaces for communication.
  90. This fits the JavaScript world nicely and makes your server-side code
  91. more dynamic - for better or for worse.