18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Web</strong> Services<br />

}<br />

onError: function (sMessage) {<br />

//...<br />

}<br />

This object is passed into the createProxyAsync() method as in the following:<br />

oFactory.createProxyAsync(“wsdl_location”, “port_name”, “”, true, oCallbackObject);<br />

The createProxyAsync() method should always be called within a try..catch statement because it<br />

throws an error if the method fails for any reason.<br />

After this method is called, a new thread is started to load the WSDL proxy. When loaded, the onLoad()<br />

method is called and the new proxy is passed in. If you are only making a single <strong>Web</strong> service call, you<br />

may want to have this method call the specific operation. Otherwise, you’ll probably want to store a reference<br />

to the proxy, such as this one, for later use:<br />

var oProxy = null;<br />

var oCallbackObject = {<br />

onLoad: function (oCreatedProxy) {<br />

oProxy = oCreatedProxy;<br />

},<br />

}<br />

onError: function (sMessage) {<br />

alert(sMessage);<br />

}<br />

In this example, a global variable named oProxy is created that stores the created proxy. In the<br />

onLoad() method, the created proxy is assigned into oProxy so it can be accessed in other functions.<br />

Even though you can specify whether the proxy operations are called synchronously or asynchronously,<br />

the synchronous calls don’t always work. It’s always best to use asynchronous calls.<br />

To call an operation asynchronously requires yet another callback object. This object must have a callback<br />

method for each operation you intend to call. The name of the callback method is always the<br />

name of the operation followed by Callback (for instance, the callback method for getTemp is called<br />

getTempCallback). This method receives as its arguments the response data from the <strong>Web</strong> service call.<br />

To understand this better, it’s best to take a look at an example.<br />

This example once again uses the Temperature Service. The important pieces of information are the<br />

WSDL file location and the port name. Here are the global variables:<br />

var oProxy = null;<br />

var sWSDL = “http://www.xmethods.net/sd/2001/TemperatureService.wsdl”;<br />

var sPort = “TemperaturePort”;<br />

Next, you need a callback object for creation of the proxy. This simply assigns the created proxy to the<br />

oProxy variable and assigns a callback object for the operations using the setListener() method:<br />

523

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!