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.

IntegratingAJavaScriptLibraryAsAnExtension.asciidoc 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ---
  2. title: Integrating A JavaScript Library As An Extension
  3. order: 39
  4. layout: page
  5. ---
  6. [[integrating-a-javascript-library-as-an-extension]]
  7. = Integrating a JavaScript library as an extension
  8. JavaScript can also be used for creating Extensions e.g. for integrating
  9. existing JavaScript libraries. See <<CreatingAUIExtension#, Creating a UI extension>> for general information about Extensions. The main
  10. difference when using JavaScript is that you extend
  11. `AbstractJavaScriptExtension`, that your shared state class should
  12. extend `JavaScriptExtensionState` and then of course that your
  13. client-side implementation is written in JavaScript. See <<IntegratingAJavaScriptComponent#, Integrating a
  14. JavaScript component>> for basic information about how to use JavaScript
  15. for your client-side logic.
  16. This tutorial will create a simple Extension for integrating
  17. https://developers.google.com/analytics/devguides/collection/gajs/[Google
  18. Analytics]. Because the Analytics API just uses the same `_gaq.push`
  19. function with different arguments, the JavaScript connector logic can be
  20. equally simple. Aside from asynchronously loading ga.js, the client-side
  21. code just adds a callback that the server-side code can use to push new
  22. commands.
  23. [source,javascript]
  24. ....
  25. window._gaq = window._gaq || [];
  26. (function() {
  27. var ga = document.createElement('script');
  28. ga.type = 'text/javascript';
  29. ga.async = true;
  30. ga.src = ('https:' == document.location.protocol ?
  31. 'https://ssl' : 'http://www') +
  32. '.google-analytics.com/ga.js';
  33. var s = document.getElementsByTagName('script')[0];
  34. s.parentNode.insertBefore(ga, s);
  35. })();
  36. window.com_example_Analytics = function() {
  37. this.pushCommand = function(command) {
  38. _gaq.push(command);
  39. }
  40. }
  41. ....
  42. The server-side Extension class provides the common Extension API for
  43. extending a UI instance as well as API for some Analytics features. All
  44. the Analytics features are based on the `pushCommand` method that
  45. invokes the corresponding client-side callback.
  46. The Analytics API used in this example has nothing that warrants using
  47. shared state, but you can of course use shared state in your own
  48. JavaScript Extension if you want to as long as your state class extends
  49. `JavaScriptExtensionState`.
  50. [source,java]
  51. ....
  52. @JavaScript("analytics_connector.js")
  53. public class Analytics extends AbstractJavaScriptExtension {
  54. public Analytics(UI ui, String account) {
  55. extend(ui);
  56. pushCommand("_setAccount", account);
  57. }
  58. public void trackPageview(String name) {
  59. pushCommand("_trackPageview", name);
  60. }
  61. private void pushCommand(Object... commandAndArguments) {
  62. // Cast to Object to use Object[] commandAndArguments as the first
  63. // varargs argument instead of as the full varargs argument array.
  64. callFunction("pushCommand", (Object) commandAndArguments);
  65. }
  66. }
  67. ....
  68. Extensions are suitable for integrating many existing JavaScript
  69. libraries that do not provide a component that is added to a layout. By
  70. using a client-side JavaScript connector for integrating the JavaScript
  71. library, you can eliminate GWT from the equation to give you slightly
  72. less code to maintain.