Callback function is very common in JavaScript; however there is no callback function in PowerScript. And in order to support the JavaScript callback function, a corresponding PowerScript event must be created and be called back instead (let's call it callback event). Which means, for each JavaScript callback function, define an event in the associated PowerBuilder object, and use @EventName to replace the JavaScript callback function name. If you do not want to use the callback event in PowerBuilder, use @, then the function will return the result directly when the execution is complete. The return value is a string (if an object is to be returned, then the object properties will be returned in a JSON string).
-
EventName is the name of the event of the object that the second parameter of the
AssociateJSwithPB
function points to. -
The event will need to have a parameter for receiving the value returned by the function.
-
If the function uses more than one callback event, either all of them are used, or none; it cannot be some used, while others not, because the event to be called back will be in asynchronous execution. For example, the following script is incorrect:
iole_contact.find ( '["name"]', "@ue_contactfind", "@", iec_option.of_ToString() )
-
When the callback event failed to execute, an error code is returned. The error code is defined in the Cordova help, and you will need to search the error code in the Cordova help to determine the cause.
-
As the callback event is executed asynchronously, do not execute
OLEObject.DisconnectObject()
right after @+EventName is used, otherwise the callback event may fail.
Here is a code example.
The JavaScript callback function
onSuccess
:
function onSuccess(contacts) { for (var i = 0; i < contacts.length; i++) { for (var j = 0; j < contacts[i].addresses.length; j++) { alert("Pref: " + contacts[i].addresses[j].pref + "\n" + "Type: " + contacts[i].addresses[j].type + "\n" + "Formatted: " + contacts[i].addresses[j].formatted + "\n" + "Street Address: " + contacts[i].addresses[j].streetAddress + "\n" + "Locality: " + contacts[i].addresses[j].locality + "\n" + "Region: " + contacts[i].addresses[j].region + "\n" + "Postal Code: " + contacts[i].addresses[j].postalCode + "\n" + "Country: " + contacts[i].addresses[j].country); } } };
The JavaScript callback function
onError
:
function onError(contactError) { alert('onError!'); };
The JavaScript method that uses these two callback functions:
//finds all contacts var options = new ContactFindOptions(); options.filter = ""; options.multiple = true; var filter = ["displayName", "addresses"]; navigator.contacts.find (filter, onSuccess, onError, options);
The PowerBuilder callback event
ue_contactfind
(the counterpart of
onSuccess
) defined in the associated PowerBuilder
object:
ue_contactfind (string as_return) return none
The PowerBuilder callback event
ue_contactfind_error
(the counterpart of
onError
) defined in the associated PowerBuilder
object:
ue_contactfind_error (string as_return) return none
The PowerBuilder code that uses these two callback events:
Oleobject lole_cordova lole_cordova = Create OleObject li_Return = lole_cordova.connecttonewobject ( "AppeonMobile.CordovaPlugin" ) //associates with the current PowerBuilder object lole_cordova.AssociateJSwithPB ("navigator.contacts", this) //ue_contactfind & ue_contactfind_error defined in "this" object (the currently associated PB object) //iec_option.of_ToString() is a JSON string ls_Return = iole_contact.find ('["name"]', "@ue_contactfind", "@ue_contactfind_error", iec_option.of_ToString() ) //or replace it with the following script, if not to use any callback event //ls_Return = iole_contact.find ('["name"]',"@", "@", iec_option.of_ToString() )