18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17<br />

oBuffer.append(“xmlns:soapenc=\”http://schemas.xmlsoap.org/soap/encoding/\” “);<br />

oBuffer.append(“xmlns:xs=\”http://www.w3.org/2001/XMLSchema\” “);<br />

oBuffer.append(“xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”>”);<br />

oBuffer.append(“”);<br />

oBuffer.append(“”);<br />

oBuffer.append(this.zipcode);<br />

oBuffer.append(“”);<br />

};<br />

return oBuffer.toString();<br />

Because the SOAP request string is so long, this method uses the StringBuffer() object created earlier<br />

in the book to build up the string. Note that the zipcode property is used here to insert the zip code in<br />

question instead of passing it in as an argument. If the zip code is passed in as an argument, you must<br />

rewrite the send() method to take this into account; this way, the send() method can be used as-is.<br />

The handleResponse() method expects to receive a SOAP response string as its only argument. Once<br />

again, this format can be determined by using the WSDL analyzer tool from XMethods:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Despite all the extra code, the only part of interest is what is contained within the element.<br />

The easiest way to extract that data from the string is to use a regular expression; there’s no need for<br />

expensive DOM parsing and operations in this case (for more complicated <strong>Web</strong> services, however, that<br />

may be an option). After the value is extracted, the parseFloat() function can be used to get the<br />

floating-point value:<br />

TemperatureService.prototype.handleResponse = function (sResponse) {<br />

var oRE = /(.*)/gi;<br />

oRE.test(sResponse);<br />

return parseFloat(RegExp[“$1”]);<br />

};<br />

The only thing left to do is modify the send() method to accept an argument (the zip code) because the<br />

zipcode property must be assigned for the method to function properly (remember, the send() method<br />

calls buildRequest(), which uses the zipcode property to create the SOAP string).<br />

To accomplish this, you first must create a pointer to the original send() method:<br />

TemperatureService.prototype.webServiceSend = TemperatureService.prototype.send;<br />

528

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

Saved successfully!

Ooh no, something went wrong!