OpenWGA 7.9 - OpenWGA Concepts and Features

Design and development » TMLScript

Collections

Collections are JavaScript objects which contain multiple other objects of any type. They are used on many places in TMLScript to collect data or pass multiple values as argument to some method or function.

TMLScript mostly works with two different collection types.

A List is a collection containing an unlimited number of objects in a defined order. Each element of the list is accessible via an index, where 0 is the index of the first element. An empty list can be created via WGA.createList():

var aList = WGA.createList();

aList.add("a");

aList.add("b");

aList.add("c");


for (var i=0; i < aList.size() ; i++) {

  log.info("Element index " + i + " contains: " + aList.get(i);

}

A Lookup table is a collection wich again may contain an unlimited number of objects but does not maintain order. Instead of an index it lets you assign a custom "lookup key" to every element by which it may be retrieved in direct access. So it resembles a table where you can lookup each element by a specific key. Key and value may be of any type (while seldomly anything beyond basic data types is used as key). An empty lookup table may be created via WGA.createLookupTable():

var aTable = WGA.createLookupTable();

aTable.put("a", 1);

aTable.put("b", 2);

aTable.put("c", 3);


var lookupKeys = WGA.getLookupKeys(aTable); // This returns a list with all the lookup keys of the map

for (var i=0; i < lookupKeys.size(); i++) {

  var key = lookupKeys.get(i);

  log.info("Lookup key " + key + " stores: " + aList.get(key);

}

Besides these TMLScript-specific collections there are still the JavaScript native collections like JavaScript arrays and JavaScript objects. While an array closely resembles a List a JavaScript object may be used like a lookup table, with property names being lookup keys and property values being the stored values.

TMLScript allows to use a JavaScript array instead of a List and a JavaScript object instead of a Lookup Table anywhere in TMLScript or WGAPI where normally a List or Lookup Table would be used. So it is mostly up to your taste as a designer which one you prefer.

Native JavaScript arrays and objects have the advantage of playing nicely with the collection facilities of JavaScript, for example the "for (x in y)" statement, and have some nice convenience methods to iterate over all elements and modify them, like "forEach()". Also they have nice inline definition syntaxes:

var anArray = ["a", "b", "c"]

var anObject = { "a":1, "b":2, "c":3 };

Downsides are that they lack some of the high-level methods that Lists and Lookup table offer (which, to be fair, can often be custom-implemented rather easily).