JavaScript Objects in RPE


With RPE 1.2.1 and newer you can store in RPE template variables more than just strings. Any JavaScript object or Java object can be stored and manipulated through RPE template variables.

A particularly useful object type is the JavaScript object mainly because of its ability to easily store and retrieve named data. This is done through the object’s properties and can be used with 0 preparations as unlike strong typed languages you do not need to define a class/structure for this.

info_ = new Object();
info_["name"] = "Rational Publishing Engine";
info_[ "majorVersion"] = 1;
info_["releaseDate"] = Date();

For more on syntax and features see W3Schools: JavaScript Objects

In the RPE contexts the JavaScript objects are useful as they can store point product data or to simply reduce the number of internal variables you need.

Below is an example of creating a JavaScript object, storing some mixed data and assigning the result to an RPE template variable. Note that the information stored in the object is heterogeneous: a string, an integer, a date.

The example template, sampleJSObjects.dta,  is available for download from RPE DeveloperWorks Wiki.

rpeactual_jsObjectsCreate

NOTE: you must set a non empty property of the object when creating it as RPE is not able to distinguish between an empty object and an empty string. If the object is empty when stored in the template variable RPE will use it as a string in the next script expression.

The object is then used to render the information in the output using the same syntax for accessing the properties:

rpeactual_javaobjects

Advertisements

Author: Dragos Cojocari

Architect for Rational Publishing Engine

6 thoughts on “JavaScript Objects in RPE”

  1. In DNG schemas, I’d like to map the about field to either the ObjectType or the literalName value of an attribute. if it is of a certain objectType and a specific attribute has a value that is x or y then map to the literalname instead. The problem I am having is that it maps to literal name and also to the Object Type and I get numerous entries for same artifact in the map and thus conditions made based on the value stored are not responding correctly. I am cycling thru all resources in a module

  2. Michele, can you post your code? Are you using the JavaScript map or the Java one? And is your problem the fact that you have more than 1 entry per key in the map?

  3. I did have more than one entry per key in the map. Here is what ended up working to remove duplicates and give me the data I needed to conditionally print Table Captions/Figure Captions with just the Caption Text in the DNG Module. Not sure if you know of a better way to do this.

    if ( resourceToType == “”)
    {
    resourceToType = new Object(); //create new object
    }

    var rTt = resourceToType[about]

    if (rTt == null)
    {
    if (name != “Information”)
    {
    //set value to Artifact Type
    resourceToType[about] = name; //map href to objectTypename;
    }
    else
    {

    //Check if information_type attribute has value of Table Caption or Figure Caption
    if ($1_name == “information_type” && literalName == null)
    {
    //no information type — set value to Artifact Type
    resourceToType[about] = name; //map href to objectTypename;
    }
    else
    {
    //information_type attribute has a value
    if (literalName != “Figure Caption” && literalName != “Table Caption”)
    {
    //choice list value Table/Figure — set value to Artifact Type
    resourceToType[about] = name; //map href to objectTypename;
    }
    else
    {
    //Table/Figure — set value to choice list value of information_type
    resourceToType[about] = literalName; //map href to attribute literalname-attribute value
    }
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s