User Guide and Reference

Data Types

Hide Navigation Pane

Data Types

Previous topic Next topic No directory for this topic  

Data Types

Previous topic Next topic Topic directory requires JavaScript JavaScript is required for the print function  

Overview

Data Typing is used throughout Information Technology. In Composer, there are two sets of data types:

 

Composer's own typing, as embodied in parameter data types
— which are used by Composer internally. Some of these are exclusive to Composer:

oNode

oUID

oIs Selected

JavaScript's typing
— not to be confused with the Composer types, though some do resemble Composer's

 

The Composer types, as you would expect, were designed to handle data for both HTML and PDF forms.

 

JavaScript is looser in its data typing, which means that you usually do not explicitly specify some data's type. Circumstances, however, may require you to type data in order to get desired results.

 

JavaScript Types

JavaScript has less types than Composer. For example, there is no such thing as a Date type. All JavaScript has are:

 

String
which is a sequence of characters, usually delimmited by straight quote marks
for example: "To be or not to be"

Number
and there is no distinction made between inegers or decimals
for example: 2, 3.142, 123e5 (scientific notation for 12300000)

Boolean
that is: true or false

JavaScript arrays

JavaScript Objects
for example: JSON objects

Undefined
A variable with no value

Null
An empty variable

 

Note: undefined is not the same type as null. This is a subtle distinction, but it does have relevance to Composer scripting, as in the a scripting example given in Visibility in Collaboration.

 

Raw Data

This is a Composer internal data type. All data is held internally in Composer as a string of characters, be it a number, date, string and so on. This can be tricky: for example there is a difference between the integer 2, the floating point value 2 and the string "2". JavaScript handles the calculation of adding an integer to a floating point gracefully, but can mishandle adding an integer to a string.

 

Dates are a potential source of problems. Please refer to the Dates topic.

 

Conversions

Because of JavaScript's loose typing, the need to convert form values hardly ever arises for Script Editor parameters. Composer looks after this for you.

 

However, if you are writing your own field references manually, you may have to convert the field's value from its native raw data value. The Public API has a number of methods to do this:

 

sfc.convertToBoolean(value)

sfc.convertToFloat(value)

sfc.convertToInteger(value)

sfc.convertToString(value)
Converts a number value (or Boolean?) into a Composer string data type.

sfc.formatDate(dateObject, dateFormat)
see Dates

sfc.getFormattedValue(noderef)
returns the value of the node according to its Composer data type

sfc.getRawValue(noderef)
the opposite of the formatted value: retaurns the raw value, irrespective of the Composer data type.

 

Explicit Conversions

You can convert String values using the public API's functions (listed above). For example:

 

var myvalue = sfc.convertToNumber(sfc.getRawValue(me));

 

This code will convert the raw value of the current field ("me") to a Number.

 

Implicit Conversion

JavaScript is a loosely typed language, and will attempt to convert data for you. The rules are a little complicated, which is why we suggest that you always try to convert to the appropriate type before you use them.

 

One simple example is a way to convert any data type into a string - simply concatenate an empty string to it. For example:

 

var var1 = 18; // var1 is a number

var var2 = var1+""; var2 is a string, because we concatenated "" to it.

 

Comparisons

Be careful when making comparisons between different data types. JavaScript will attempt to convert them for you, but the results may not always be the expected.

 

In JavaScript, the "this" object generally refers to the current object receiving the event. Never use the "this" object in Composer scripts.

 

All Composer JavaScripts are embedded in a function (see Advanced Scripting), rather than being coded directly under an event. (The reasons for this are out-of-scope.) The commonly used "this" object to refer to the current object, is not available in functions. Instead, Composer provides a "me" object. The "me" object refers to the object where the Composer script is located, i.e. the form element whose Edit Properties dialog holds the script.

 

If you have some existing JavaScript code that works correctly in either PDF or HTML, you can generally simply replace every occurrence of "this" with "me" for the same code to work in Composer.

 

Note: If you want the script to work correctly across different technologies, you must also rewrite it to use Composer's technology independent JavaScript library rather than the native libraries. for example, use sfc.getRawValue(me) rather than me.rawValue. See the Avoka Composer Framework.