JavaScript

JavaScript is the preferred scripting language for TDI (and as of TDI 6.1, the only scripting language). It is fairly easy to learn, and it provides access to all the underlying Java facilities (though using some of those is not so easy!)

Reference

The definitive description of the language is the JavaScript Core Reference

For a more gentle introduction, you could try The Core JavaScript Guide or this JavaScript tutorial by Peter Kantor.

And finally, a great Quick Ref can be found here.

Most books about JavaScript assume that you want to embed it in web pages. This is not what TDI does, so if looking for books make sure that they concentrate on 'server-side JavaScript'.

Hints

Using the TDI API

Be aware that most of the results you get by invoking TDI-specific objects are Java objects (not JavaScript objects). This means that their behaviour can sometimes appear extremely odd.

For example, if you call work.getString("name") to get the value of the name attribute, what you get is a Java string object. If you compare it directly with another string (i.e. java.lang.String) returned by the same method, it does not work as expected. This is because whenever you work with Java objects, you are actually working with pointers (called "references") to the actual objects in memory. As a result, the comparison below is not between the values of these Attributes, but actually a test to see if the returned references are pointing to the same place in memory.

    var isEqual = (work.getString("name") == conn.getString("name"));

If you want to use JavaScript comparison operators, you have a couple of options: One way is to force the strings to be cast (read: "converted") to JavaScript string objects, since JavaScript variables can be directly compared. A quick-and-dirty way of forcing this conversion is by mixing in a JavaScript variable or literal value in your expression. This will cause the JavaScript engine to convert everything in the expression to JavaScript types.

    var isEqual = (work.getString("name") == (conn.getString("name") + "");

However, it is recommended that you do this this kind of typecasting with explicit code:

    var isEqual = (work.getString("name") == String(conn.getString("name")));

Another option is to use the Java String methods for string comparison:

    var isEqual = work.getString("name").equals( conn.getString("name") );

There is still a hazard though: if there was no name attribute, then work.getString("name") will return null. When this is converted to a JavaScript type it will result in the string value "null" - probably not what you were expecting!

Caseless string comparison

To compare two string objects (a and b) without regard to case:

        (a.toLowerCase() == String( b.toLowerCase() ))

Or, using the Java String comparison method instead:

        (a.equalsIgnoreCase( b ))

-- AndrewFindlay 16 March 2006
Topic revision: r6 - 04 Feb 2011, AndrewFindlay - This page was cached on 02 Apr 2020 - 19:36.

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TDI Users? Send feedback