|
Modules
A detailed introduction to the Module Service.
IntroductionHemi 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 ReadingGeneral ApproachGiven some piece of JavaScript: function MyFunction(){
}The Worker, Module, and Test Module services load and instrument framework support to the script. Module ServiceThe Test Module service extends the Module service, so prior to diving into the details of the test module it's useful to start with the general module. 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 reference, 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]);
|