src/cm/media/js/lib/yui/yui_3.10.3/docs/io/weather.html
author Yves-Marie Haussonne <ymh.work+github@gmail.com>
Fri, 09 May 2014 18:35:26 +0200
changeset 656 a84519031134
parent 525 89ef5ed3c48b
permissions -rw-r--r--
add link to "privacy policy" in the header test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Request XML data from Yahoo! Weather</title>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
    <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
    <script src="../../build/yui/yui-min.js"></script>
    
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
    <div id="hd">
        <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
    </div>
    

            <h1>Example: Request XML data from Yahoo! Weather</h1>
    <div class="yui3-g">
        <div class="yui3-u-3-4">
            <div id="main">
                <div class="content"><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 <code>http:&#x2F;&#x2F;weather.yahooapis.com&#x2F;forecastrss</code>.  To try out the example, fill in your five-digit US zip code, or Location ID.</p>
<p><strong>Please note:</strong> This example will not function on iOS devices due to the usage of Flash as the cross-domain transport.  This example may not work on older Android devices, as well.</p>
</div>
<div class="example">
<form id="wForm">
<fieldset>
    <label>Zip Code or Location ID</label> <input type="text" id="zip" value="94089">
    <p>Please enter a U.S. Zip Code or a Location ID to get the current temperature.  The default is Zip Code 94089 for Sunnyvale, California; its location ID is: USCA1116.</p>
</fieldset>
<div id="weatherModule">
    <li>Weather RSS data will appear here.</li>
</div>
<input type="button" value="Get Weather RSS" id="getWeather" disabled="disabled">
</form>


<script language="javascript">

YUI({ filter:'raw' }).use("io-xdr", "node",

    function(Y) {

        //Get a Node reference to the div we'll use for displaying
        //results:
        var div = Y.one('#weatherModule');

        //Configure the cross-domain transport:
        var xdrConfig = {
            id:'flash', //We'll reference this id in the xdr configuration of our transaction.
            src:'../../build/io-xdr/io.swf' //Relative path to the .swf file from the current page.
        };
        Y.io.transport(xdrConfig);

        //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");
        }

        //Provide a function that can help debug failed
        //requests:
        function failureHandler(id, o){
            Y.log("Failure handler called; http status: " + o.status, "info", "example");
            div.set("innerHTML", o.status + " " + o.statusText);
        }

        //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 request:
            var request = Y.io(sUrl, {
                method:"GET",
                xdr: {
                    use:'flash', //This is the xdrConfig id we referenced above.
                    dataType:'xml' //Indicate the data are XML, not string.
                },              
                on:
                    {
                        success:successHandler,
                        failure:failureHandler
                    }
                }
            );
        }

        //Add the click handler to the Get Weather RSS button as soon
        //as the Flash transport has loaded:
        Y.on('io:xdrReady', function() {
            var btn = Y.one("#getWeather");
            btn.set("disabled", false);
            //Use the Event Utility to wire the Get RSS button
            //to the getModule function.
            Y.on("click", getModule, "#getWeather");
        });

        Y.log("When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger/console.", "info", "example");
    }
);
</script>

</div>

<h3 class="first">Exploring the Code for This Example</h3>
<p>Create a YUI instance, using IO, for this example:</p>

<pre class="code prettyprint">&#x2F;&#x2F;Create a YUI instance including support for cross-domain IO:
YUI().use(&quot;io-xdr&quot;, &quot;node&quot;, function(Y) {
    &#x2F;&#x2F; Y is the YUI instance.
    &#x2F;&#x2F; The rest of the following code is encapsulated in this
    &#x2F;&#x2F; anonymous function.
} );

&#x2F;&#x2F;Configure the cross-domain transport:
var xdrConfig = {
    id:&#x27;flash&#x27;, &#x2F;&#x2F;We&#x27;ll reference this id in the xdr configuration of our transaction.
    src:&#x27;..&#x2F;..&#x2F;build&#x2F;io-xdr&#x2F;io.swf&#x27; &#x2F;&#x2F;Relative path to the .swf file from the current page.
};
Y.io.transport(xdrConfig);</pre>


<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>

<pre class="code prettyprint">&#x2F;&#x2F;Define a function to handle a successful response from
&#x2F;&#x2F;Yahoo! Weather.  The success handler will find the response
&#x2F;&#x2F;object in its second argument:
function successHandler(id, o){
    Y.log(&quot;Success handler called; handler will parse the retrieved XML and insert into DOM.&quot;, &quot;info&quot;, &quot;example&quot;);
    var root = o.responseXML.documentElement;
    var oTitle = root.getElementsByTagName(&#x27;description&#x27;)[0].firstChild.nodeValue;
    var oDateTime = root.getElementsByTagName(&#x27;lastBuildDate&#x27;)[0].firstChild.nodeValue;
    var descriptionNode = root.getElementsByTagName(&#x27;description&#x27;)[1].firstChild.nodeValue;

    div.set(&quot;innerHTML&quot;, &quot;&lt;p&gt;&quot; + oTitle + &quot;&lt;&#x2F;p&gt;&quot; + &quot;&lt;p&gt;&quot; + oDateTime + &quot;&lt;&#x2F;p&gt;&quot; + descriptionNode);

    Y.log(&quot;Success handler is complete.&quot;, &quot;info&quot;, &quot;example&quot;);
}</pre>


<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> &mdash; 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>

<pre class="code prettyprint">&#x2F;&#x2F;When the Get RSS button is clicked, this function will fire
&#x2F;&#x2F;and compose&#x2F;dispatch the IO request:
function getModule(){
    &#x2F;&#x2F;Get the input value:
    var iZip = Y.one(&#x27;#zip&#x27;).get(&quot;value&quot;);

    &#x2F;&#x2F;Create a querystring from the input value:
    var queryString = encodeURI(&#x27;?p=&#x27; + iZip);

    &#x2F;&#x2F;The Yahoo! Weather feed.
    var entryPoint = &#x27;http:&#x2F;&#x2F;weather.yahooapis.com&#x2F;forecastrss&#x27;;

    &#x2F;&#x2F;Compile the full URI for the request:
    var sUrl = entryPoint + queryString;

    Y.log(&quot;Submitting request; zip code: &quot; + iZip, &quot;info&quot;, &quot;example&quot;);

    &#x2F;&#x2F;Make the reqeust:
    var request = Y.io(sUrl, {
        method:&quot;GET&quot;,
        on:
            {
                success:successHandler,
                failure:failureHandler
            }
        }
    );
}


&#x2F;&#x2F;Add the click handler to the Get Weather RSS button as soon
&#x2F;&#x2F;as the Flash transport has loaded, indicated by the firing
&#x2F;&#x2F;of event &quot;io:xdrReady&quot;.
Y.on(&#x27;io:xdrReady&#x27;, function() {
    var btn = Y.one(&quot;#getWeather&quot;);
    btn.set(&quot;disabled&quot;, false);
    &#x2F;&#x2F;Use the Event Utility to wire the Get RSS button
    &#x2F;&#x2F;to the getModule function.
    Y.on(&quot;click&quot;, getModule, &quot;#getWeather&quot;);
});</pre>


<h4>Full Script Source</h4>

<p>Here is the full JavaScript source for this example:</p>

<pre class="code prettyprint">&lt;form id=&quot;wForm&quot;&gt;
&lt;fieldset&gt;
    &lt;label&gt;Zip Code or Location ID&lt;&#x2F;label&gt; &lt;input type=&quot;text&quot; id=&quot;zip&quot; value=&quot;94089&quot;&gt;
    &lt;p&gt;Please enter a U.S. Zip Code or a Location ID to get the current temperature.  The default is Zip Code 94089 for Sunnyvale, California; its location ID is: USCA1116.&lt;&#x2F;p&gt;
&lt;&#x2F;fieldset&gt;
&lt;div id=&quot;weatherModule&quot;&gt;
    &lt;li&gt;Weather RSS data will appear here.&lt;&#x2F;li&gt;
&lt;&#x2F;div&gt;
&lt;input type=&quot;button&quot; value=&quot;Get Weather RSS&quot; id=&quot;getWeather&quot; disabled=&quot;disabled&quot;&gt;
&lt;&#x2F;form&gt;


&lt;script language=&quot;javascript&quot;&gt;

YUI({ filter:&#x27;raw&#x27; }).use(&quot;io-xdr&quot;, &quot;node&quot;,

    function(Y) {

        &#x2F;&#x2F;Get a Node reference to the div we&#x27;ll use for displaying
        &#x2F;&#x2F;results:
        var div = Y.one(&#x27;#weatherModule&#x27;);

        &#x2F;&#x2F;Configure the cross-domain transport:
        var xdrConfig = {
            id:&#x27;flash&#x27;, &#x2F;&#x2F;We&#x27;ll reference this id in the xdr configuration of our transaction.
            src:&#x27;..&#x2F;..&#x2F;build&#x2F;io-xdr&#x2F;io.swf&#x27; &#x2F;&#x2F;Relative path to the .swf file from the current page.
        };
        Y.io.transport(xdrConfig);

        &#x2F;&#x2F;Define a function to handle a successful response from
        &#x2F;&#x2F;Yahoo! Weather.  The success handler will find the response
        &#x2F;&#x2F;object in its second argument:
        function successHandler(id, o){
            Y.log(&quot;Success handler called; handler will parse the retrieved XML and insert into DOM.&quot;, &quot;info&quot;, &quot;example&quot;);
            var root = o.responseXML.documentElement;
            var oTitle = root.getElementsByTagName(&#x27;description&#x27;)[0].firstChild.nodeValue;
            var oDateTime = root.getElementsByTagName(&#x27;lastBuildDate&#x27;)[0].firstChild.nodeValue;
            var descriptionNode = root.getElementsByTagName(&#x27;description&#x27;)[1].firstChild.nodeValue;

            div.set(&quot;innerHTML&quot;, &quot;&lt;p&gt;&quot; + oTitle + &quot;&lt;&#x2F;p&gt;&quot; + &quot;&lt;p&gt;&quot; + oDateTime + &quot;&lt;&#x2F;p&gt;&quot; + descriptionNode);

            Y.log(&quot;Success handler is complete.&quot;, &quot;info&quot;, &quot;example&quot;);
        }

        &#x2F;&#x2F;Provide a function that can help debug failed
        &#x2F;&#x2F;requests:
        function failureHandler(id, o){
            Y.log(&quot;Failure handler called; http status: &quot; + o.status, &quot;info&quot;, &quot;example&quot;);
            div.set(&quot;innerHTML&quot;, o.status + &quot; &quot; + o.statusText);
        }

        &#x2F;&#x2F;When the Get RSS button is clicked, this function will fire
        &#x2F;&#x2F;and compose&#x2F;dispatch the IO request:
        function getModule(){
            &#x2F;&#x2F;Get the input value:
            var iZip = Y.one(&#x27;#zip&#x27;).get(&quot;value&quot;);

            &#x2F;&#x2F;Create a querystring from the input value:
            var queryString = encodeURI(&#x27;?p=&#x27; + iZip);

            &#x2F;&#x2F;The Yahoo! Weather feed.
            var entryPoint = &#x27;http:&#x2F;&#x2F;weather.yahooapis.com&#x2F;forecastrss&#x27;;

            &#x2F;&#x2F;Compile the full URI for the request:
            var sUrl = entryPoint + queryString;

            Y.log(&quot;Submitting request; zip code: &quot; + iZip, &quot;info&quot;, &quot;example&quot;);

            &#x2F;&#x2F;Make the request:
            var request = Y.io(sUrl, {
                method:&quot;GET&quot;,
                xdr: {
                    use:&#x27;flash&#x27;, &#x2F;&#x2F;This is the xdrConfig id we referenced above.
                    dataType:&#x27;xml&#x27; &#x2F;&#x2F;Indicate the data are XML, not string.
                },              
                on:
                    {
                        success:successHandler,
                        failure:failureHandler
                    }
                }
            );
        }

        &#x2F;&#x2F;Add the click handler to the Get Weather RSS button as soon
        &#x2F;&#x2F;as the Flash transport has loaded:
        Y.on(&#x27;io:xdrReady&#x27;, function() {
            var btn = Y.one(&quot;#getWeather&quot;);
            btn.set(&quot;disabled&quot;, false);
            &#x2F;&#x2F;Use the Event Utility to wire the Get RSS button
            &#x2F;&#x2F;to the getModule function.
            Y.on(&quot;click&quot;, getModule, &quot;#getWeather&quot;);
        });

        Y.log(&quot;When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger&#x2F;console.&quot;, &quot;info&quot;, &quot;example&quot;);
    }
);
&lt;&#x2F;script&gt;</pre>

</div>
            </div>
        </div>

        <div class="yui3-u-1-4">
            <div class="sidebar">
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                        <li data-description="Use IO to request data over HTTP.">
                                            <a href="get.html">HTTP GET to request data</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Use IO to request XML data from a remote web service.">
                                            <a href="weather.html">Request XML data from Yahoo! Weather</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
                                            <a href="xdr.html">Request JSON using Yahoo! Pipes</a>
                                        </li>
                                    
                                
                                    
                                
                            </ul>
                        </div>
                    </div>
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples That Use This Component</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                        <li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
                                            <a href="../overlay/overlay-io-plugin.html">IO Plugin</a>
                                        </li>
                                    
                                
                            </ul>
                        </div>
                    </div>
                
            </div>
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

<script>
YUI.Env.Tests = {
    examples: [],
    project: '../assets',
    assets: '../assets/io',
    name: 'weather',
    title: 'Request XML data from Yahoo! Weather',
    newWindow: '',
    auto:  false 
};
YUI.Env.Tests.examples.push('get');
YUI.Env.Tests.examples.push('weather');
YUI.Env.Tests.examples.push('xdr');
YUI.Env.Tests.examples.push('overlay-io-plugin');

</script>
<script src="../assets/yui/test-runner.js"></script>



</body>
</html>