Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

gwt-connector.asciidoc 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ---
  2. title: Integrating the Two Sides with a Connector
  3. order: 4
  4. layout: page
  5. ---
  6. [[gwt.connector]]
  7. = Integrating the Two Sides with a Connector
  8. A client-side widget is integrated with a server-side component with a
  9. __connector__. A connector is a client-side class that communicates changes to
  10. the widget state and events to the server-side.
  11. A connector normally gets the state of the server-side component by the __shared
  12. state__, described later in
  13. <<dummy/../../../framework/gwt/gwt-shared-state#gwt.shared-state,"Shared
  14. State">>.
  15. [[gwt.connector.basic]]
  16. == A Basic Connector
  17. The basic tasks of a connector is to hook up to the widget and handle events
  18. from user interaction and changes received from the server. A connector also has
  19. a number of routine infrastructure methods which need to be implemented.
  20. ----
  21. @Connect(MyComponent.class)
  22. public class MyComponentConnector
  23. extends AbstractComponentConnector {
  24. @Override
  25. public MyComponentWidget getWidget() {
  26. return (MyComponentWidget) super.getWidget();
  27. }
  28. @Override
  29. public MyComponentState getState() {
  30. return (MyComponentState) super.getState();
  31. }
  32. @Override
  33. public void onStateChanged(StateChangeEvent stateChangeEvent)
  34. {
  35. super.onStateChanged(stateChangeEvent);
  36. // Do something useful
  37. final String text = getState().text;
  38. getWidget().setText(text);
  39. }
  40. }
  41. ----
  42. Here, we handled state change with the crude [methodname]#onStateChanged()#
  43. method that is called when any of the state properties is changed. A finer and
  44. simpler handling is achieved by using the [classname]#@OnStateChange# annotation
  45. on a handler method for each property, or by [classname]#@DelegateToWidget# on a
  46. shared state property, as described later in
  47. <<dummy/../../../framework/gwt/gwt-shared-state#gwt.shared-state,"Shared
  48. State">>.
  49. [[gwt.connector.communication]]
  50. == Communication with the Server-Side
  51. The main task of a connector is to communicate user interaction with the widget
  52. to the server-side and receive state changes from the server-side and relay them
  53. to the widget.
  54. Server-to-client communication is normally done using a __shared state__, as
  55. described in
  56. <<dummy/../../../framework/gwt/gwt-shared-state#gwt.shared-state,"Shared
  57. State">>, as well as RPC calls. The serialization of the state data is handled
  58. completely transparently.
  59. ifdef::web[]
  60. Once the client-side engine receives the changes from the server, it reacts to
  61. them by creating and notifying connectors that in turn manage widgets. This is
  62. described in
  63. <<dummy/../../../framework/gwt/gwt-advanced#gwt.advanced.phases,"Client-Side
  64. Processing Phases">> in more
  65. detail.
  66. endif::web[]
  67. For client-to-server communication, a connector can make remote procedure calls
  68. (RPC) to the server-side. Also, the server-side component can make RPC calls to
  69. the connector. For a thorough description of the RPC mechanism, refer to
  70. <<dummy/../../../framework/gwt/gwt-rpc#gwt.rpc,"RPC Calls Between Client- and
  71. Server-Side">>.