Configuring a SOAP service is a very simple action to take. It needs two parameters only. Lets see the picture first;

ws-1

  • Service name, unique names between same type service is a “should”. If you keep this issue in account, it becomes whatever you like, giving proper names will be helpful later on though
  • WSDL(Web Service Definiton Language) adress.

Configuring a SOAP is this simple, while you reading these lines, native side of the service already became ready.

Before starting to code javascript, we have to examine the service a bit.  Look at methods that are defined inside WSDL of the service for parameter keys and types, types can be primitive or complex, our gui does not show operation names for the time being.

Ok, go check the http://soaptest.parasoft.com/calculator.wsdl, you will see standart math operations. For this example we will use “multiply”. Also the parameter names for the operation are “x”,”y” as you can see in the wsdl. If it’s your service, no need to mention parameter names of course. That is the only essential part of the usage, if you don’t use form generator tool, I will mention it a bit in bottom but you can check form  generation tool section for further information.

Lets start with standart service handling;

———CalculatorService.js———

/**
 *Just to mention, if you read ServiceObjects - Rest already, its same.
 */
var operations; // To hold incoming operations
var isOperationsReady = false; // Check


//successCallback
function sCallback(a){
 //Actual code goes here...
 alert(JSON.stringify(a));
}

//errorCallback
function eCallback(error){
 //Actual code goes here...
 alert(error);
}

/**
*Call service with payload.
*/

function multiply(payload){
 if(!isOperationsReady){//Create operations first. This part is essential
 Livo.ServiceObjects.getServiceObject(
 "WSServiceObject-Calculator",// Your service name, after you added it as Calculator.
 {},// Configuration object, Soap services do not need any configuration.
 function(success){
 operations = success;// success is the js object that holds service so you have operations.multiply(); now.
 isOperationsReady = true;
 operations.multiply(payload,sCallback,eCallback);// call it here for the first time to be sure its called after operations created. Async design of javascript.
 },
 function(error){
 // do whatever you want with the error message , means there is no such a service, no internet connection etc.
 });
 }else{
 operations.multiply(payload,sCallback,eCallback);
 
 }
}

———index.html———

<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>App Name</title>
<script src="cordova.js"></script>
<script src="CalculatorService.js"></script>
<script type="text/javascript">
 
 document.addEventListener("deviceready", onDeviceReady, false);
 function onDeviceReady() {
  navigator.splashscreen.hide();// if you use splash screen, close it.
  var a = {"x":"5","y"="2"}; // payload
  multiply(a);
 } 
 
</script>
</head>
<body>
</body>
</html>

That is all, you will see an alert in the screen that says, {result:”10″},  key name result comes from service itself.

Lets talk about complex services which define complex types such as person that has multiple fields. Lets say person type has name,lastName, dateOfBorn etc as fields. For us it’s just a simple json object such as this;

{name:"Livo", lastName:"Love", dateOfBorn:"1993"}

this is your person here, lets imagine a service method which takes two parameters “cityName” and “person” for searching inside a city. Now then, your payload is like this;

 

var payload = { cityName:",Istanbul", person:{name:"Livo", lastName:"Love", dateOfBorn:"now"} }

 

This idea can go deeper recursively but there is no need to worry about complexity so much because services generally start with simple methods and returns more complex objects and you can use these returns for other methods.

Form Generator Tool

Form Generator Tool generates service library to create forms inside a HTML element you gave the id of. Find service name inside Service Management section, choose application name you want to add javascript file of the service after pushing generate button, add it to your html. It handles, form validation, data types and data names in your mobile application. Checkout Form Generator Tool documentation for further information.

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *