|
Utilities
Detailed Introduction to Utilities
Hemi JavaScript Framework UtilitiesHemi.appThe Hemi.app utilities include convenience methods for creating and initializing Application Components, Application Spaces, XHTML Components, and the Window and Window Manager components. Due to the synchronicity of certain activities - specifically using the methods for Window and Window Manager, the Message Service delivery delay feature should not be enabled. TBD: Example of bootstrapping asynchronous manager, window, template loading. Hemi.objectAccessorsThe Hemi.object.addObjectAccessor method instruments an object instance for a managed object list of a named type. Decorated objects do not need to participate in or conform to the Hemi Framework API. Private accessor values are stored in the objects property (created if it doesn't exist). Public accessor methods are instrumented on the object. Given some accessor, Accessor, the method signatures are:
For example, for an Accessor of thing, the following methods would be injected:
When an object is added to the Accessor array, several properties are set:
The following example adds the accessors for 'thing' to an empty object: Hemi.include("hemi.object");
var oSomeObject = {};
Hemi.object.addObjectAccessor(oSomeObject, "thing");Once the accessors are added, the object may now store and retrieve objects via the 'thing' accessor moniker. var oThing = {};
oSomeObject.addNewThing(oThing, "thing_1");
/// After adding, oThing will now have the object_id, access_index, and access_name properties
///
var sId = oThing.object_id;
/// Retrieve by id:
///
var oLookupById = oSomeObject.getThing(sId);
/// Retrieve by name:
///
var oLookupByName = oSomeObject.getThingByName("thing_1");
Hemi.eventObject Scope Buffering for EventsHemi includes a utility for preserving scope across anonymous events where the object scope may otherwise be lost. The scope buffer leverages the Object Registry to resolve a given object, and instruments an API on the object to automatically resolve the scope. Hemi.include("hemi.event");
var oSomeObject = {};
// prepare for and register with the framework
Hemi.prepareObject("SomeObject","1.0",oSomeObject,true);
// Instrument the scope buffer API, which creates the scopeHandler method.
//
Hemi.event.addScopeBuffer(oSomeObject);
// Create a scoped handler named 'someaction'
// This assumes the handler is named "_handle_someaction"
// And injects a new handler named "_prehandle_someaction"
oSomeObject.scopeHandler("someaction", 0, 0, true);
oSomeObject._handle_someaction = function(){
// ...
}
// Instead of providing oSomeObject._handle_someaction,
// use oSomeObject._prehandle_someaction
//
doSomeMethodOrEventCallback(oSomeObject._prehandle_someaction);Hemi.cssEmbedded JSON in CSS Class NamesWhen standards restrict the extending elements with custom attributes (EG: Hemi's convenience attributes used by XHTMLComponent), configuration can be embedded into inline CSS class names. The decodeJSON method in the hemi.css utility class can be used to extract the data. The syntax is: {#HF_NAME{name:value}}Where the principle object property is NAME and its members the child values. For example: var o = Hemi.css.decodeJSON("{#HF_Demo{attr:value}}");
o.Demo.attr == "value";When using Application Spaces (as easy as including hemi.app.space), the class names are discoverable through the SpaceObject CSS map. The following example identifies all CSS class names with encoded JSON for XHTML nodes that were loaded as XHTMLComponent objects, and then iterate through all objects that matched that class name. <p class = "#{HF_para{custom:value}}">paragraph</p>
<script type = "text/javascript">
Hemi.message.service.subscribe("onspaceconfigload", function (s, v) {
if (!v.is_primary) return;
var oSpace = Hemi.app.space.service.getPrimarySpace();
for (var sClass in oSpace.space_css_map){
if (!sClass || !sClass .match(/\\{/)) continue;
var oEmbedded = Hemi.css.decodeJSON(sClass);
if(!oEmbedded ) continue;
var aObj = oSpace.getSpaceObjectsByClass(sClass);
for(var p = 0;p<aObj.length;p++){
var oObj = aObj[p];
/// Here, oObj may be configured with data encoded in the CSS class.
}
}
});
</script>Hemi.util.loggerThe Hemi.util.logger utility decorates an object with a logging facility, and registers a named notification block. Refer to the Message Service introduction for details about the encoded notification syntax which the logger masks. The following example demonstrates how to register an object. Hemi.include("hemi.util.logger");
var oSomeObject = {};
Hemi.util.logger.addLogger(oSomeObject, "SomeObject", "Some Object Log", 999);
/// Equivalent to invoking sendMessage to 999.3
SomeObject.log("Test Normal");
/// Equivalent to invoking sendMessage to 999.4
oSomeObject.logWarning("Test Warning");
/// Equivalent to invoking sendMessage to 999.5.3.1
oSomeObject.logError("Test Error","3.1");For any subscribers to the onsendmessage publication, an example of the message raised by the log call may be: /// hh:mm:ss:mil::LEVEL: ShortName (block.level.context.condition). Message 13:36:23:972::ERROR: SomeObject (999.5.3.1). Test Error |