Skip to content

Module Service

StephenCote edited this page Sep 30, 2013 · 2 revisions

Introduction

Hemi includes a set of services for loading external script files which may or may not conform to the Hemi Framework API, and use those scripts as managed DOM Worker threads via the Worker Service, encapsulated modules via the Module Service, and encapsulated tests via the Test Module Service.

Related Reading

Approach

Given some piece of JavaScript:

function MyFunction(){

}

Loading this script via the Worker, Module, or Test Module services instrument framework support into the script.

Module Service

The Module service is extended by the Worker and Test Module service, and indirectly used by the Task Service.

If the previous script was saved to /SomeDir/myscript.js, the NewModule method can be used to load the script as a reusable module and return a new instance of the module.

{{{ var oModule = Hemi.app.module.service.NewModule("myscript", null, "/SomeDir/"); }}}

The previous oModule variable is a Framework-managed instance of the script defined in myscript.js, inheriting the Module and Framework Object interfaces, and is registered with the Registry Service.

A module includes an anonymous reference to itself for ease of self-discovery, initialization and destruction virtual methods, and may be connected with an HTML element via the Container property. The following example reflects the instrumented Module API.

function MyFunction(){
   /// Use 'Module' to refer to the instance
   Module.MyCustomMethod();
}
this.MyCustomMethod = function(){
   /// Use 'Container' to refer to any bound HTML element
   if(this.Container) this.Container.innerHTML = "Changed the content";
};
this.Initialize = function(){

};
this.Unload = function(){

};

The Module Base is available directly from the Module Service, and includes an array of implementations. The following demonstrates how the previous implementation is discoverable from the implementations array.

var oModuleBase = Hemi.app.module.getModuleByName("myscript");
/// assuming the module instance still exists ...
var oModule = Hemi.registry.service.getObject(oModuleBase.Impls[0]);

Clone this wiki locally