您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UsingRPCFromJavaScript.asciidoc 3.9KB

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