JBoss Community Archive (Read Only)

GateIn Portal 3.8

GMD Custom Adapters

JavaScript does not have a standard module until Ecmascript 6, several proposal exist for defining modules all revolving around the same module pattern. GateIn decided to use the AMD format because it is really adapted to the web and its asynchronous nature.

As consequence sometimes you can have to deal with included script that do not match the self-executing function declaration format or requirejs format expected by GateIn and is RequireJS integration. Custom code is required for adapting the script to the expected format. To provide this bit of flexibility it is possible to declare an adapter that will wrap the adapted script.

The jQuery library is a good example of how a custom adapter is useful, thanks to the adapter we can reuse the jQuery without any change, easing the integration and maintenance of this library in GateIn. jQuery uses the following construct for defining itself:

(function(window, undefined) {
})(window);

The main issue with this construct is that it will bind jQuery to the window but most importantly it will not return any value as expected by the dependency system. Thanks to the custom adapter we can integrate it trivially:

<module>
 <name>jquery</name>
 <script>
   <adapter>
     (function() {
       <include>/jquery.js</include>
       return jQuery.noConflict(true);
     })();
   </adapter>
 </script>
</module>

The adapter tag can contains mixed content and the include tag will perform a mere inclusion (as C language includes) of the original jQuery script in the resulting module:

define("SHARED/jquery", [], function() {
  return (function() {
    (function(window, undefined) {
    })(window);
   return jQuery.noConflict(true);
  })();
});
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 13:20:44 UTC, last content change 2014-04-15 06:39:44 UTC.