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.5KB

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