Namespace.js

From LugdunonWiki
Revision as of 16:53, 15 February 2015 by SuperHawksman (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Engine Content: This information applies to the engine and therefore to all servers.

At the core of Lugdunon is the Namespace object. This object provides for an object oriented model in JS, as well as runtime code/class dependency resolution. It also provides support for internationalization (i18n), asset loading, and local caching of assets.


Let’s take a look at what a simple class representing a 2dPoint (in net/lugdunon/math/TwoDPoint.js) would look like:


  1. Namespace.package("net.lugdunon.math");
  2. Namespace.newClass("net.lugdunon.math.TwoDPoint");
  3.  
  4. net.lugdunon.math.TwoDPoint.prototype.init=function(initData)
  5. {
  6.     this.x=initData.x;
  7.     this.y=initData.y;
  8.  
  9.     return(this);
  10. };
  11.  
  12. net.lugdunon.math.TwoDPoint.prototype.add=function(p)
  13. {
  14.     if(p.instanceOf && p.instanceOf("net.lugdunon.math.TwoDPoint"))
  15.     {
  16.         this.x+=p.x;
  17.         this.y+=p.y;
  18.     }
  19. };


The net/lugdunon/math/TwoDPoint.js file starts off by declaring a package via Namespace.package() which will cause Namespace to create a hierarchical object namespace if it doesn’t already exist. Next, we declare the new class via the Namespace.newClass() function call. This will create the new class object for us and ready it for extension. The next step is to write the class’ init function. The init(initData) function is a very important one in Namespace.js and must follow a few conventions:


  • It must take a single argument that usually contains a js object.
  • It must complete with a return(this); statement.


Following that, we declare the class’ only function, add, which accepts another TwoDPoint object and adds the x and y coordinate to itself.


A simple 3dPoint class (in com/fusionhealth/math/ThreeDPoint.js) that extends TwoDPoint would look like this:


  1. Namespace.package("net.lugdunon.math");
  2. Namespace.require("net.lugdunon.math.TwoDPoint");
  3. Namespace.newClass("net.lugdunon.math.ThreeDPoint","net.lugdunon.math.TwoDPoint");
  4.  
  5. net.lugdunon.math.ThreeDPoint.prototype.init=function(initData)
  6. {
  7.     this.callSuper(net.lugdunon.math.ThreeDPoint,"init",[initData]);
  8.  
  9.     this.z=initData.z;
  10.  
  11.     return(this);
  12. };
  13.  
  14. net.lugdunon.math.ThreeDPoint.prototype.add=function(p)
  15. {
  16.     if(p.instanceOf && p.instanceOf("net.lugdunon.math.ThreeDPoint"))
  17.     {
  18.         this.callSuper(net.lugdunon.math.ThreeDPoint,"add",[p]);
  19.         this.z+=p.z;
  20.     }
  21. };


There are some new concepts introduced there, so let’s take a look at them. On the second line we’ll find the Namespace.require(“net.lugdunon.math.TwoDPoint”); call. This call tells the framework that we will be using the TwoDPoint class in this file, and the framework automatically resolves and loads the source for TwoDPoint if it hasn’t been previously loaded by another class. There is now a 2nd parameter used in the newClass() function. The second parameter indicates the class you are extending, which in this case is TwoDPoint. In the init and add functions we see a call to this.callSuper(). This call takes in the current class id, the function name and an array of arguments and calls the parent class’ (of the class referenced by class id) function.


Namespace.js also provides support for loading html, css, and internationalization. To load an html or css file, use the Namespace.requireCSS() and Namespace.requireHTML(). The structure of their arguments is similar to the Namespace.require() function. For instance, to load html content located in net/lugdunon/math/LinearAlgebra.html, the call would resemble Namespace.requireHTML(“net.lugdunon.math.LinearAlgebra”).


For loading an i18n resource, the famework expects a collection of i18n resources at a given namespace (directory path). For instance, if you were wanting to add i18n features to the math package, you could designate net.lugdunon.math.i18n as the i18n namespace and the respective translated i18n resources would be placed in net/lugdunon/math/i18n/. US English would be located in net/lugdunon/math/i18n/en_US.js, Canadian French in net/lugdunon/math/i18n/fr_CA.js and so forth. To load an i18n resource for the math package, you would call Namespace.requireI18NResource(“net.lugdunon.math.i18n”). The framework will then load the appropriate language file for the end user’s locale.