Chapter 38. Custom Adapters
Until ECMAScript 6, there had been no standardized format of a JavaScript module declaration. However, several proposals of such a format have emerged, all revolving around the same module pattern. The AMD (Asynchronous Module Definition) format was chosen because it is suitable for the Web, and its asynchronous nature.
As a consequence, you sometimes have to deal with included scripts that do not match the self-executing function declaration format. To adapt such scripts to the expected format without editing the actual code, it is possible to declare an adapter that will wrap the original scripts and make them reusable.
The jQuery library is a good example of how a custom adapter is useful. Thanks to the adapter, it is possible to reuse the jQuery library without any modifications, which facilitates the integration and maintenance of the library in the portal. jQuery uses the following construct to define itself:
(function(window, undefined) { })(window)
The issue with this construct is that it will bind jQuery to the window, and that it will not return any value as expected by the dependency system. Thanks to the custom adapter, we can integrate JQuery the following way:
<module> <name>jquery</name> <script> <adapter> (function() { <include>/jquery.js</include> return jQuery.noConflict(true); })(); </adapter> </script> </module>
The
<adapter> element can encapsulate any code, and the <include> element will perform a simple string inclusion of the original jQuery script:
define("SHARED/jquery", [], function() { return (function() { (function(window, undefined) { })(window); return jQuery.noConflict(true); })(); });