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

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