Namespace.js
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:
- Namespace.package("net.lugdunon.math");
- Namespace.newClass("net.lugdunon.math.TwoDPoint");
- net.lugdunon.math.TwoDPoint.prototype.init=function(initData)
- {
- this.x=initData.x;
- this.y=initData.y;
- return(this);
- };
- net.lugdunon.math.TwoDPoint.prototype.add=function(p)
- {
- if(p.instanceOf && p.instanceOf("net.lugdunon.math.TwoDPoint"))
- {
- this.x+=p.x;
- this.y+=p.y;
- }
- };
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:
- Namespace.package("net.lugdunon.math");
- Namespace.require("net.lugdunon.math.TwoDPoint");
- Namespace.newClass("net.lugdunon.math.ThreeDPoint","net.lugdunon.math.TwoDPoint");
- net.lugdunon.math.ThreeDPoint.prototype.init=function(initData)
- {
- this.callSuper(net.lugdunon.math.ThreeDPoint,"init",[initData]);
- this.z=initData.z;
- return(this);
- };
- net.lugdunon.math.ThreeDPoint.prototype.add=function(p)
- {
- if(p.instanceOf && p.instanceOf("net.lugdunon.math.ThreeDPoint"))
- {
- this.callSuper(net.lugdunon.math.ThreeDPoint,"add",[p]);
- this.z+=p.z;
- }
- };
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.