OpenWGA 7.6 - OpenWGA Concepts and Features
Design and development » TMLScript » TMLScript Objects » TMLScript Objects V1TMLScript globals V1
TMLScript globals are special variables that are automatically available to a wide range of TMLScript code across all requests and sessions.
Normal TMLScript globals are available to all TMLScript code of the complete OpenWGA runtime. They are dedicated tools to enhance the language capabilities of TMLScript with new features that should be globally available.
So called TMLScript DB globals are available to all TMLScript code of a single OpenWGA application. They are usable to provide resources that are needed across all code of that application.
An OpenWGA design can define TMLScript globals that may contain one of these four artefacts:
- A Java Package for easily referencing classes inside this package
- Any Java object
- Any TMLScript object, including Custom TMLScript objects
Because of their scope TMLScript globals should always contain resources that are meaningful for the whole application or runtime, not for single users or sessions. Also they should not keep references on resources which are valid only for special requests oder sessions, like TMLContext objects, TMLForms, TMLPortlets or TMLUserProfiles.
Registering globals
TMLScript globals have case-sensitive names that follow strict naming rules to avoid collisions with other TMLScript qualifiers:
- The name must begin with an uppercase letter
- The name must not equal the name of a WebTML metadata field
- The name must not equal the name of a predefined JavaScript object like "Object", "String" etc.
TMLScript globals can be registered in two ways:
The Design configuration of an OpenWGA design contains a registry called "Design Shortcuts" which can be used to register normal TMLScript globals for Java packages.
Otherwise it is possible to register TMLScript globals directly from TMLScript. Use method Design.registerGlobal() for normal globals and Design.registerDbGlobal() for db globals. Both methods take the global name and the resource to store as global for arguments:
WGA.design().registerGlobal("Tools", Packages.de.innovationgate.tools);
WGA.design().registerDbGlobal("AppTools", WGA.createObject("objects:apptools"));
It is recommended to register globals in the connect script of an application (see Tab "Design Configuration"). The registration of TMLScript globals from script running on behalf of WebTML pages is discouraged, as this promotes inappropriate usage with user/session bound resources.
Using globals
TMLScript globals are available to the TMLScript code of their scope just by using their name in exact case. The contained resource then can be used just like it would have been addressed in its normal form:
So when registering a Java package:
WGA.design().registerGlobal("WGAPI", Packages.de.innovationgate.webgate.api);
Then the following code can be used to access Java classes contained in the package. Here by using a static method on class "de.innovationgate.webgate.api.WGFactory":
var wgapiTempDir = WGAPI.WGFactory.getTempDir();
An example storing a Java object in a DB global:
var appContext = new Packages.de.innovationgate.myapp.ApplicationContext();
WGA.design().registerDbGlobal("AppContext", appContext);
The object then can be used under name "AppContext" in all TMLScript code of the application:
return AppContext.getStatus();
The same is possible for a TMLScript object:
var dbm = WGA.createObject("objects:dbm");
WGA.design().registerGlobal("DBM", dbm);