2.14.2.1. Declaring a module

This section explains how a module can be integrated and consume dependencies, for example, with other modules. A GateIn module consists of the declaration of a JavaScript self-executing function and its declaration in the gatein-resources.xml descriptor of the web application. This descriptor defines the module and its dependencies. At runtime, GateIn will build a graph of resources and their dependencies. When a client needs a particular module, it will invoke a JS function that calls RequireJS to resolve dependencies.

For instance, if you have a foo module that uses the jQuery dependency:

(function ($) {
  // Do something with jQuery
})(jQuery)

The module is declared as a self-executing function in the foo.js file. This file is then declared in the gatein-resources.xml file:


...
<module>
  <name>foo</name>
  <script>
    <path>/foo.js</path>
  </script>
  <depends>
    <module>jquery</module>
    <as>jQuery</as>
  </depends>
</module>
...

The self-executing function declares:

The self-executing function argument must match the dependencies:

Note

The argument of the self-executing function is a JavaScript constructor which aims to pass arguments to the function, let the function name the arguments as they want, and override the current scope of execution. For example, jQuery uses it:

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

Resources are related to the concept of dependency relationship that specifies how scripts are related to others and how the modular system should work. In this example, the foo module needs to use jQuery, it means that they are in relationship: the foo module depends on jQuery. When the module is loaded, the jQuery module must be available to the module.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus