amd.rst 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. AMD
  2. ###
  3. .. toctree::
  4. :maxdepth: 2
  5. Overview
  6. ========
  7. There are two JavaScript system in Elgg: the deprecated 1.8 system, and the newer `AMD (Asynchronous Module
  8. Definition) <http://requirejs.org/docs/whyamd.html>`_ compatible system introduced in 1.9.
  9. This discusses the benefits of using AMD in Elgg.
  10. Why AMD?
  11. ========
  12. We have been working hard to make Elgg's JavaScript more maintainable and useful.
  13. We made some strides in 1.8 with the introduction of the "``elgg``" JavaScript object and library, but
  14. have quickly realized the approach we were taking was not scalable.
  15. The size of `JS on the web is growing
  16. <http://httparchive.org/trends.php?s=All&minlabel=Feb+11+2011&maxlabel=Feb+1+2013#bytesJS&reqJS>`_
  17. quickly, and JS in Elgg is growing too. We want Elgg to be able to offer a solution that makes JS
  18. development as productive and maintainable as possible going forward.
  19. The `reasons to choose AMD <http://requirejs.org/docs/whyamd.html>`_ are plenteous and
  20. well-documented. Let's highlight just a few of the most relevant reasons as they relate to Elgg
  21. specifically.
  22. 1. Simplified dependency management
  23. -----------------------------------
  24. AMD modules load asynchronously and execute as soon as their dependencies are available, so this
  25. eliminates the need to specify "priority" and "location" when registering JS libs in Elgg. Also, you
  26. don't need to worry about explicitly loading a module's dependencies in PHP. The AMD loader (RequireJS in this
  27. case) takes care of all that hassle for you. It's also possible have
  28. `text dependencies <http://requirejs.org/docs/api.html#text>`_ with the RequireJS text plugin,
  29. so client-side templating should be a breeze.
  30. 2. AMD works in all browsers. Today.
  31. ------------------------------------
  32. Elgg developers are already writing lots of JavaScript. We know you want to write more. We cannot
  33. accept waiting 5-10 years for a native JS modules solution to be available in all browsers before
  34. we can organize our JavaScript in a maintainable way.
  35. 3. You do not need a build step to develop in AMD.
  36. --------------------------------------------------
  37. We like the edit-refresh cycle of web development. We wanted to make sure everyone developing in
  38. Elgg could continue experiencing that joy. Synchronous module formats like Closure or CommonJS just
  39. weren't an option for us. But even though AMD doesn't require a build step, *it is still very
  40. build-friendly*. Because of the ``define()`` wrapper, it's possible to concatenate multiple modules
  41. into a single file and ship them all at once in a production environment. [#]_
  42. AMD is a battle-tested and well thought out module loading system for the web today. We're very
  43. thankful for the work that has gone into it, and are excited to offer it as the standard solution
  44. for JavaScript development in Elgg starting with Elgg 1.9.
  45. .. [#] This is not currently supported by Elgg core, but we'll be looking into it since reducing round-trips is critical for a good first-view experience, especially on mobile devices.