weather.mustache revision f57277abdd5505c315d47f339271d01bdd4c91f9
<style type="text/css" scoped>
#weatherModule li {margin-left:2em;}
#weatherModule { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-bottom:1em;}
</style>
<div class="intro">
<p>This example demonstrates how to use IO to make a cross-domain request to `http://weather.yahooapis.com/forecastrss`.</p>
<p>To try out the example, fill in your five-digit US zip code, or Location ID.</p>
</div>
<div class="example">
{{>io-weather-source}}
</div>
<h3 class="first">Exploring the Code for This Example</h3>
<p>Create a YUI instance, using IO, for this example:</p>
```
//Create a YUI instance including support for IO:
YUI().use("io-base", "node", function(Y) {
// Y is the YUI instance.
// The rest of the following code is encapsulated in this
// anonymous function.
} );
```
<h4>Callback Object and the Weather RSS</h4>
<p><a href="http://developer.yahoo.com/weather/">Yahoo! Weather RSS</a> will return an XML document if the transaction is successful. The following <code>success</code> callback handlers is used to process the response.</p>
```
//Define a function to handle a successful response from
//Yahoo! Weather. The success handler will find the response
//object in its second argument:
function successHandler(id, o){
Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example");
var root = o.responseXML.documentElement;
var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue;
var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue;
var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue;
div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode);
Y.log("Success handler is complete.", "info", "example");
}
```
<h4>Assemble the Querystring and Initiate the Transaction</h4>
<p>The Yahoo! Weather RSS feed requires a simple HTTP GET request with a base URL and a querystring containing the required information as a name-value pair. In this example, we will use the following parameter:</p>
<ul>
<li><strong>p</strong> — location as U.S. Zip Code or Location ID</li>
</ul>
<p>The following are some example Location IDs (do not include the city name):</p>
<ul>
<li><strong>Beijing</strong>: <em>CHXX0008</em></li>
<li><strong>Helsinki</strong>: <em>FIXX0002</em></li>
<li><strong>London</strong>: <em>UKXX0085</em></li>
<li><strong>Moscow</strong>: <em>RSXX0063</em></li>
<li><strong>Munich</strong>: <em>GMXX0087</em></li>
<li><strong>Paris</strong>: <em>FRXX0076</em></li>
<li><strong>Riyadh</strong>: <em>SAXX0017</em></li>
<li><strong>Tokyo</strong>: <em>JAXX0085</em></li>
</ul>
<p>For more details on the Yahoo! Weather RSS feed and other location IDs, please visit <a href="http://developer.yahoo.com/weather/index.html">http://developer.yahoo.com/weather/index.html</a>.
<p>Function <code>getModule</code> retrieves the input values for location and creates a querystring:</p>
```
//When the Get RSS button is clicked, this function will fire
//and compose/dispatch the IO request:
function getModule(){
//Get the input value:
var iZip = Y.one('#zip').get("value");
//Create a querystring from the input value:
var queryString = encodeURI('?p=' + iZip);
//The Yahoo! Weather feed.
var entryPoint = 'http://weather.yahooapis.com/forecastrss';
//Compile the full URI for the request:
var sUrl = entryPoint + queryString;
Y.log("Submitting request; zip code: " + iZip, "info", "example");
//Make the reqeust:
var request = Y.io(sUrl, {
method:"GET",
on:
{
success:successHandler,
failure:failureHandler
}
}
);
}
//Use the Event Utility to wire the Get RSS button
//to the getModule function:
Y.on("click", getModule, "#getWeather");
```
<h4>Full Script Source</h4>
<p>Here is the full JavaScript source for this example:</p>
```
{{>io-weather-source}}
```