OpenWGA 7.10 - OpenWGA Concepts and Features

Design and development » TMLScript » TMLScript Objects » TMLScript Objects V1

Custom TMLScript objects V1

Custom TMLScript objects are objects defined in TMLScript modules that carry certain parts of needed functionality for the design. They are actually a special way to create custom JavaScript objects with some enhanced features.

While it would be possible to define all neccessary processes for a design inside WebTML actions and other TMLScript code it sometimes is beneficial to group them together to custom TMLScript objects which then can be used by your design just like those objects that are provided by the OpenWGA platform

The definition of a TMLScript object is stores as a TMLScript module which contains property and method definitions for the object. Here is an example:

// Defining a private property, only visible from inside the object

var privVar = "x";


// defining a public property. Accessible from outside

this.pubVar = "y";


// The constructor, must be of name "init"

this.init = function(param1, param2) {

  privVar = param1; // Accessing private variables

  this.pubVar = param2;  // Accessing public variables

}


// Declaring a private method, only usable from inside the object

function privateFunc(param1) {

  log.info("Called private function with param " + param1);

}


// Declaring a public method, usable from outside

this.publicFunc = function(parm1) {
 privateFunc(parm1); // Aufruf einer privaten Methode
}

TMLScript objects are created using method WGA.createObject(). It takes the name of the defining TMLScript module as first parameter, using the same syntax like WGA.callAction() would: the suffix is omitted and the colon ":" is used as path divider. Parameters to the constructor are added from parameter 2 on.

Assuming that the object definition from above was stored under "objects/myobject.tmlscript" this would mean:

var myObj = createObject("objects:myobject", "a", 2);

The constructor is optional, but if it is defined then it is executed on object creation, taking the parameters 2 to n from the createObject call.

The code that constructed the object now can use public properties and methods:

myObj.publicFunc(6);
log.info("Feld pubVar des Objektes enthält: " + myObj.pubVar);


If multiple objects of the same type need to be constructed then it is more effective to first load an object definition and use it to instantiate those objects.

Use method Design.loadObjectDefinition() for the design that contains the object definition to load it. It takes the module name in the same syntax:

var myObjDef = loadObjectDefinition("objects:myobject");

The result is a precompiled definition object that can be given to WGA.createObject() instead of the module name:

var obj1 = createObject(myObjDef, "a", 1);

var obj2 = createObject(myObjDef, "b", 2);

var obj3 = createObject(myObjDef, "c", 3);


Some more notes on custom TMLScript objects

1.) The "this" object inside the code of the object does NOT represent the current TMLContext but the object itself. There still is an implicit TMLContext available but only with omitting the "this" qualifier. This TMLContext represents the base context of the script that currently uses the TMLScript object. It is NOT the context of object creation.

2.) If your custom TMLScript object retrieves more resources from the design that defines it - like labels or definitions of more custom TMLScript objects - then you need to create a Design context for your current object to let TMLScript correctly retrieve these resources from the right design. Use method WGA.design() with your object as parameter to get such a design context. You can also store this design context as property on your object to reuse it for many occasions.

4.) TMLScript objects can be stored to WebTML variables, including session variables. However they should not keep references to resources of the current WebTML request, like TMLForm, TMLUserProfile or TMLPortlet objects if they persist beyond that request, as those objects  will be in an invalid state once the request ends.