Code examples

Let's take Contact Cordova plugin as an example to show you how to add a contact to the mobile device and how to write the PowerBuilder code.

The corresponding Cordova help can be found here: https://www.npmjs.com/package/cordova-plugin-contacts.

Example 1: To add the following contact to the device:

name: Tester

phone number: 15845562007, 07552698563

Step 1: Create a PowerBuilder OLEObject object and call the ConnectToNewObject function to connect it to the AppeonMobile.CordovaPlugin object.

Oleobject ole_cordova
ole_cordova = Create OLEObject
ole_cordova.ConnectToNewObject ("AppeonMobile.CordovaPlugin")

Step 2: Call the AppeonMobile.CordovaPlugin's AssociateJSwithPB function to associate the JavaScript object with the current PowerBuilder object.

navigator.contacts is the name of the JavaScript object defined in the Contact Cordova plugin. You can get this name from the Cordova help.

this represents the object where the callback function used by navigator.contacts should be transferred to.

ole_cordova.AssociateJSwithPB ("navigator.contacts", this)

Step 3: Create a new Contact object -- mycontact.

Cordova help has this: "The navigator.contacts.create method is synchronous, and returns a new Contact object."

ole_cordova.ExecJavaScript ('var mycontact = navigator.contacts.create();')

Step 4: Set the name of the new contact person to "Tester".

According to the Cordova help, the Contact object has three properties for name: displayName, name, & nickname. We choose to use nickname to set the new contact's name. These three properties are supported differently by different platform. You can find this out in the Cordova help and determine which property to use according to your request.

ole_cordova.ExecJavaScript ('mycontact.nickname = "Tester";')

Step 5: Set the phone number of the new contact person to "15845562007" & "07552698563".

According to the Cordova help, the Contact object has ContactField[] (an array) for phoneNumbers.

Create the ContactField object first. According to Cordova help, the ContactField object has three properties: type, value, & pref. Then set the phone number to 15845562007:

ole_cordova.ExecJavaScript ("var p1 = new ContactField();") //creates the object
ole_cordova.ExecJavaScript ("p1.type = 'home';") //value is a string, so include it in quotes
ole_cordova.ExecJavaScript ("p1.value = '15845562007';")
ole_cordova.ExecJavaScript ("p1.pref = true;")

And set the phone number to 07552698563:

ole_cordova.ExecJavaScript ("var p2 = new ContactField();")
ole_cordova.ExecJavaScript ("p2.type = 'work';")
ole_cordova.ExecJavaScript ("p2.value = '07552698563';")
ole_cordova.ExecJavaScript ("p2.pref = false;")

Finally, set the values in an array for the phoneNumbers property:

ole_cordova.ExecJavaScript ("mycontact.phoneNumbers = [p1,p2];")

Step 6: Save the new Contact object and the values.

According to the Cordova help, after the new Contact object is created, it must be saved using the save() method in order for the values to be saved into the database.

The save() method uses callback functions, so it cannot be executed using ExecJavaScript. We need to associate with the mycontact object to execute the save() method.

ole_cordova.AssociateJSwithPB ("mycontact", this)

String ls_Return //for receiving the returned value from save(). 
//The new contact information that is successfully saved will be returned in a JSON string.
ls_Return = ole_cordova.save ("@","@") //we choose not to use callback function

Example 2: to write the PowerBuilder code according to the JavaScript code.

The following table shows you how to write the PowerBuilder code that will execute and get the same result as the JavaScript code.

The JavaScript code uses ContactField to set phoneNumbers, and it is copied from the Cordova help: https://www.npmjs.com/package/cordova-plugin-contacts.

Table 5. JavaScript-to-PowerScript mapping

JavaScript PowerScript

None

The following preparation is required in the PowerBuilder only.

Create an OLEObject object, connect it to AppeonMobile.CordovaPlugin, and associate the PB object with the JS object.

Oleobject ole_cordova
ole_cordova = Create OLEObject
ole_cordova.ConnectToNewObject ("AppeonMobile.CordovaPlugin")
ole_cordova.AssociateJSwithPB ("navigator.contacts", this)

Create a new contact:

var contact = navigator.contacts.create();

Use ExecJavaScript to directly execute the JavaScript code:

ole_cordova.ExecJavaScript ('var contact = navigator.contacts.create();')

Store contact phone numbers in ContactField[]:

var phoneNumbers = [];
 phoneNumbers[0] = new ContactField ('work', '212-555-1234', false);
 phoneNumbers[1] = new ContactField ('mobile', '917-555-5432', true);
 phoneNumbers[2] = new ContactField ('home', '203-555-7890', false);
contact.phoneNumbers = phoneNumbers;

Use ExecJavaScript to directly execute the JavaScript code:

ole_cordova.ExecJavaScript ('var phoneNumbers = [];')
ole_cordova.ExecJavaScript ('phoneNumbers[0] = new ContactField ("work", "212-555-1234", false);')
ole_cordova.ExecJavaScript ('phoneNumbers[1] = new ContactField ("mobile", "917-555-5432", true);')
ole_cordova.ExecJavaScript ('phoneNumbers[2] = new ContactField ("home", "203-555-7890", false);')
ole_cordova.ExecJavaScript ('contact.phoneNumbers = phoneNumbers;')

Save the contact:

contact.save();

Associate the JavaScript Contact object with the PB OLEObject object, and then use dot notation to execute save().

save() uses callback functions, so it cannot be executed by ExecJavaScript.

ole_cordova.AssociateJSwithPB ("contact", this)
String ls_Return //for receiving the save() returned value
//the new contact information will be returned in a JSON string
ls_Return  = ole_cordova.save ("@", "@")