AJILE

From Wikipedia, the free encyclopedia

AJILE, pronounced "Agile", is the Advanced JavaScript Importing & Loading Extension.

Ajile

Ajile's Official Site
Developer: Michael A. I. Lee
Latest release: 0.7.9 / March 11, 2007
OS: Cross-platform
Use: JavaScript
License: MPL 1.1 / LGPL 2.1 / GPL 2.0
Website: ajile.iskitz.com

Ajile is an open-source JavaScript module that extends the JavaScript language with the namespace support it does not inherently provide.

Ajile allows developers to create JavaScript modules with unique namespaces and clearly defined dependencies. These scripts can be automatically loaded and/or imported as necessary for reuse and interoperability.

Contents

[edit] Defining a Namespace

Ajile provides the Namespace directive for defining namespaces. The following example demonstrates its use:

   Namespace ("com.iskitz.ajile.examples");

   com.iskitz.ajile.examples.NamespaceExample = function()
   {
      alert("This is the NamespaceExample!");
   };

In the above example the com.iskitz.ajile.examples namespace is created and the NamespaceExample module is added to it. A reversed domain name is used as the namespace to assure that the module is uniquely named. The ability to uniquely identify modules is especially important when using multiple modules from varied sources within the same page or site.

When working with modules within an Intranet or non-Internet environment, it may be sufficient to use simpler namespaces. Ajile provides the flexibility to create as simple or as complex a namespace as desired.

[edit] Namespace Importing

As effective as namespaces can be in promoting interoperability, they can complicate development by requiring the use of long module names. Ajile addresses this issue by providing the Import directive.

The Import directive provides these key benefits:

  • Simple names to reference uniquely named JavaScript modules.
  • Simple definition of JavaScript module dependencies.
  • Simple programmatic loading of external JavaScript modules.

[edit] Example

The following code demonstrates how the Import directive is used:

   Import ("com.iskitz.ajile.examples.Complex");

   function testImport()
   {
      var complex = new Complex();
      complex.sayHello();
   }

The Import directive in the code above indicates that there is a dependency that must be imported. The externally defined com.iskitz.ajile.examples.Complex.js JavaScript module is automatically imported and made accessible via its short name, Complex. The imported Complex module is then used by the testImport function.

[edit] Complex Module

The following is the content of the Complex module:

   Namespace ("com.iskitz.ajile.examples");
   Import    ("com.iskitz.ajile.examples.Simple");

   com.iskitz.ajile.examples.Complex = function()
   {
      var simple = new Simple();

      this.sayHello = function sayHello()
      {
         var message = "Hello World!\n\nThis is a " + this.toString()
                       + " object that imported and is\nusing a "
                       + simple.toString() + " object!";

         alert(message);
      };

      this.toString = function toString()
      {
         return "[Complex]";
      };
   };

The Import directive in the code above automatically imports the externally defined com.iskitz.ajile.examples.Simple.js JavaScript module then makes it available via its short name Simple.

[edit] Simple Module

The following is the content of the Simple module:

   Namespace ("com.iskitz.ajile.examples");

   com.iskitz.ajile.examples.Simple = function()
   {
      this.toString = function toString()
      {
         return "[Simple]";
      };
   };

[edit] Namespace Importing with Aliases

When importing multiple modules it is possible to encounter naming conflicts. Ajile provides the ImportAs directive as a solution to this problem.

The ImportAs directive provides two benefits in addition to those provided by the Import directive:

  • Flexibility to import a module using any un-used short name.
  • Ability to handle naming conflicts by assigning aliases to similarly named modules.

[edit] Example

The following code demonstrates how the ImportAs directive can be used to handle module naming conflicts:

   Import   ("com.iskitz.ajile.examples.Complex");
   ImportAs ("AComplex", "com.iskitz.ajile.examples.ambiguity.Complex");

   function testAmbiguity()
   {
      var complex = new Complex();
      complex.sayHello();

      var aComplex = new AComplex();
      aComplex.sayHello();

      return false;
   }

In the code above two modules with identical short names Complex are imported. The Import directive automatically imports the externally defined com.iskitz.ajile.examples.Complex.js JavaScript module as is. The ImportAs directive also automatically imports the externally defined com.iskitz.ajile.examples.ambiguity.Complex.js JavaScript module but performs the extra step of assigning it the AComplex alias.

By assigning the AComplex alias to the imported com.iskitz.ajile.examples.ambiguity.Complex module, the testAmbiguity() function is able to reference both modules without encountering any naming conflicts.

[edit] AComplex Module

The following is the content of the AComplex module:

   Namespace ("com.iskitz.ajile.examples.ambiguous");
   Import    ("com.iskitz.ajile.examples.Simple");

   com.iskitz.ajile.examples.ambiguous.Complex = function()
   {
      var simple = new Simple();

      this.sayHello = function sayHello()
      {
         var message = "Hello World!\n\nThis is an ambiguous " + this.toString()
                     + " object that\nimported and is using a "
                     + simple.toString() + " object!";

         alert(message);
      };

      this.toString = function toString()
      {
         return "[Complex]";
      };
   };

[edit] External links