Using complex types (JavaScript)

Overview

There is a number of complex types that can be defined in the RODL Library:

The actual implementation of these classes differs on different RemObjects SDK platforms.

JavaScript way

Due to the typeless nature of JavaScript, extra type information should be stored to properly serialize values for passing over the network. A wrapper objects is used to store type information along with the values.

ROComplexType descendants implement the toObject() and fromObject() methods to convert between wrapper objects and shallow JavaScript objects easily.

Enums

x = new TSex(){value : "sxMale"}
x.toObject():
"sxMale"

Backward (same for other complex types):

x.fromObject("sxFemale");

Structs

x = new TPerson(){
    "Age":{"dataType":"Integer","value":"33"},
    "FirstName":{"dataType":"AnsiString","value":"John"},
    "LastName":{"dataType":"AnsiString","value":"Doe"},
    "Sex":{
        "dataType":"TSex",
        "value":{"value":"sxMale"}
        }
    }
x.toObject():
{"Age":"33","FirstName":"John","LastName":"Doe","Sex":"sxMale"}

Arrays

x = new AnsiStringArray(){"elementType":"AnsiString","items":["one", "two"]}
x.toObject():
["one", "two"]

Exceptions

someService.someExceptionThrowingMethod(
    function(result) {
        console.log(result);
    }, 
    function(msg, e) {
        console.log(e.name);
        console.log(e.message);
        console.log(e.fields.field1.value);
        console.log(e.fields.field2.value);
    }
);

e.fields is a ROStructType, so e.fields.toObject() could be used too.

Type Declaration

Complex types are declared in the auto-generated _intf file by the JavaScript codegen.

// Enum: TSex
function TSex() {
    this.value = null;
};
TSex.prototype = new RemObjects.SDK.ROEnumType();
TSex.prototype.enumValues = [
    "sxMale",
    "sxFemale"
    ];
TSex.prototype.constructor = TSex;
RemObjects.SDK.RTTI["TSex"] = TSex;
// Struct: TPerson
function TPerson() {
    this.Age = {dataType : "Integer", value : null};
    this.FirstName = {dataType : "AnsiString", value : null};
    this.LastName = {dataType : "AnsiString", value : null};
    this.Sex = {dataType : "TSex", value : null};
};
TPerson.prototype = new RemObjects.SDK.ROStructType();
TPerson.prototype.constructor = TPerson;
RemObjects.SDK.RTTI["TPerson"] = TPerson; 

Usage

var myPerson = new TPerson();

myPerson.fromObject({FirstName : document.getElementById("editFN").value,
    LastName : document.getElementById("editLN").value,
    Age : document.getElementById("editAg").value,
    Sex : new TSex()});
myPerson.Sex.value.value = document.getElementById("editSx").value;

Service.EchoPerson(myPerson, 
    function(result) {
        alert(result);
    }, 
    RemObjects.UTIL.showError);