source: cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/examples/io/io-xdr.html @ 5259

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/examples/io/io-xdr.html@5259
Revision 5259, 18.6 KB checked in by domlowe, 11 years ago (diff)

adding yui javascript

Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2<html>
3<head>
4        <title>YUI Library Examples: IO Utility: Cross-Domain JSON Transaction &mdash; Retrieving a News Feed from Yahoo! Pipes</title>
5    <meta http-equiv="content-type" content="text/html; charset=utf-8">
6        <link rel="stylesheet" type="text/css" href="../../assets/yui.css" >
7
8<style>
9    /*Supplemental CSS for the YUI distribution*/
10    #custom-doc { width: 95%; min-width: 950px; }
11    #pagetitle {background-image: url(../../assets/bg_hd.gif);}
12    #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}
13</style>
14
15<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css">
16<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" />
17<script type="text/javascript" src="../../build/yui/yui-min.js"></script>
18<style type="text/css">
19#output li {margin-left:2em;}
20#output { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-top:1em;}
21</style>
22</head>
23<body id="yahoo-com" class=" yui-skin-sam">
24<div id="custom-doc" class="yui-t2">
25<div id="hd">
26        <div id="ygunav">
27                <p>
28            <em>
29                <a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> 
30            </em>
31                </p>
32                <form action="http://search.yahoo.com/search" id="sitesearchform">
33            <input name="vs" type="hidden" value="developer.yahoo.com">
34            <input name="vs" type="hidden" value="yuiblog.com">
35                    <div id="sitesearch">
36                        <label for="searchinput">Site Search (YDN &amp; YUIBlog): </label>
37                            <input type="text" id="searchinput" name="p">
38                            <input type="submit" value="Search" id="searchsubmit" class="ygbt">
39                    </div>
40                </form>
41    </div>
42        <div id="ygma"><a href="../../"><img src="../../assets/logo.gif"  border="0" width="200" height="93"></a></div>
43        <div id="pagetitle"><h1>YUI Library Examples: IO Utility: Cross-Domain JSON Transaction &mdash; Retrieving a News Feed from Yahoo! Pipes</h1></div>
44</div>
45<div id="bd">
46
47        <div id="bar-note"><p><strong>Note:</strong> This is YUI 3.x. Looking for <a href="http://developer.yahoo.com/yui/">YUI 2.x</a>?</p></div>
48
49        <div id="yui-main">
50                <div class="yui-b">
51                  <div class="yui-ge">
52                          <div class="yui-u first example" id="main">
53
54        <h2>IO Utility: Cross-Domain JSON Transaction &mdash; Retrieving a News Feed from Yahoo! Pipes</h2>
55
56        <div id="example" class="promo">
57        <p>
58        <p>In the example below, IO is employed to make a cross-domain request to <a href="http://pipes.yahoo.com">Yahoo! Pipes</a>.  The output of the Pipe is an RSS-style feed formatted as JSON.  We pass that output to the JSON Utility's <code>parse</code> method for sanitization and then display the contents of the Pipe in a list.</p>
59
60<p>The cross-domain approach obviates the need for a server-side proxy, making it faster. And the use of IO in place of a script node allows us to retrieve the JSON data as a string and execute <code>JSON.parse</code> against it, making it safer to use; a script node would evaluate immediately in the global scope as soon as it was loaded.</p>        </p>   
61
62        <div class="module example-container ">
63                        <div class="hd exampleHd">
64                        <p class="newWindowButton yui-skin-sam">
65                <a href="io-xdr_clean.html" target="_blank">View example in new window.</a>
66            </p>
67                </div>          <div id="example-canvas" class="bd">
68
69               
70        <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->
71       
72        <button id="fetch" disabled="disabled">Load JSON RSS news feed from Yahoo! Pipes.</button>
73
74<div id="output">
75        <ul>
76                <li>Content from Yahoo! Pipes feed will display here.</li>
77    </ul>
78</div>
79
80<script language="javascript">
81
82YUI({base:"../../build/", timeout: 10000}).use("io", "substitute", "json-parse",
83
84        function(Y) {
85
86                //Data fetched will be displayed in a UL in the
87                //element #output:
88                var output = Y.Node.get("#output ul");
89
90                //Configure the cross-domain protocol:
91                var xdrConfig = {
92                        id:'flash', //We'll reference this id in the xdr configuration of our transaction.
93                        yid: Y.id,  //The yid provides a link from the Flash-based XDR engine
94                                                //and the YUI instance.
95                        src:'../../build/io/io.swf?t=' + new Date().valueOf().toString() //Relative path to the .swf file from the current page.
96                };
97                Y.io.transport(xdrConfig);
98
99                //Event handler called when the transaction begins:
100                var handleStart = function(id, a) {
101                        Y.log("io:start firing.", "info", "example");
102                        output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>");
103                }
104
105                //Event handler for the success event -- use this handler to write the fetched
106                //RSS items to the page.
107                var handleSuccess = function(id, o, a) {
108
109                        //We use JSON.parse to sanitize the JSON (as opposed to simply eval'ing
110                        //it into the page):
111                        var oRSS = Y.JSON.parse(o.responseText);
112
113                        //From here, we simply access the JSON data from where it's provided
114                        //in the Yahoo! Pipes output:
115                        if (oRSS && oRSS.count) {
116
117                                var s = "<!--begin news stories fetched via Yahoo! Pipes-->",
118                                        //t in this case is our simple template; this is fed to
119                                        //Y.Lang.substitute as we loop through RSS items:
120                                        t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>";
121
122                                for (var i=0; i<oRSS.count; i++) {
123                                        s += Y.Lang.substitute(t, oRSS.value.items[i]);
124                                }
125
126                                //Output the string to the page:
127                                output.set("innerHTML", s);
128                                output.addClass("yui-null");
129
130                        } else {
131                                //No news stories were found in the feed.
132                                var s = "<li>The RSS feed did not return any items.</li>";
133                        }
134                }
135
136                //In the event that the HTTP status returned is > 399, a
137                //failure is reported and this function is called:
138                var handleFailure = function(id, o, a) {
139                        Y.log("ERROR " + id + " " + a, "info", "example");
140                }
141
142                //With all the aparatus in place, we can now configure our
143                //io call.
144                var cfg = {
145                        method: "GET",
146                        xdr: {
147                                use:'flash' //This is the xdrConfig id we referenced above.
148                        },
149                        on: {
150                                //Our event handlers previously defined:
151                                start: handleStart,
152                                success: handleSuccess,
153                                failure: handleFailure
154                        }
155                };
156
157                //Wire the buttton to a click handler to fire our request each
158                //time the button is clicked:
159                var handleClick = function(o) {
160                        Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example");
161                        var obj = Y.io(
162                                //this is a specific Pipes feed, populated with cycling news:
163                                "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
164                                cfg
165                        );
166                }
167
168                //add the clickHandler as soon as the xdr Flash module has
169                //loaded:
170                Y.on('io:xdrReady', function() {
171                        var fetch = Y.Node.get("#fetch");
172                        fetch.set("disabled", false);
173                        Y.on("click", handleClick, fetch);
174                });
175
176        }
177);
178</script>       
179        <!--END SOURCE CODE FOR EXAMPLE =============================== -->
180       
181               
182                </div>
183        </div>                 
184        </div>
185               
186        <h2 class="first">Implementing a Simple Cross-Domain Request for JSON Data</h2>
187
188<p>In this example, we begin with a YUI instance that loads the IO and JSON modules:</p>
189
190<textarea name="code" class="JScript" cols="60" rows="1">
191//Create a YUI instance including support for IO and JSON modules:
192YUI({base:"../../build/", timeout: 10000}).use("io", "substitute", "json-parse", function(Y) {
193        // Y is the YUI instance.
194        // The rest of the following code is encapsulated in this
195        // anonymous function.
196} );
197</textarea>
198<p>We'll also get a Node reference to the container we'll be using to output the data we retrieve:</p>
199
200<textarea name="code" class="JScript" cols="60" rows="1">//Data fetched will be displayed in a UL in the
201//element #output:
202var output = Y.Node.get("#output ul");</textarea>
203
204<p>Next, we configure IO's cross-domain interface for this YUI instance.  Flash is the underlying mechansim used here.  Be sure to configure the <code>src</code> property to point to your <code>IO.swf</code> file.</p>
205
206<textarea name="code" class="JScript" cols="60" rows="1">//Configure the cross-domain protocol:
207var xdrConfig = {
208    id:'flash', //We'll reference this id in the xdr configuration of our transaction.
209    yid: Y.id,  //The yid provides a link from the Flash-based XDR engine
210                //and the YUI instance.
211    src:'../../build/io/IO.swf?t=' + new Date().valueOf().toString() //Relative path to the .swf file from the current page.
212};
213Y.io.transport(xdrConfig);</textarea>
214
215<p>The configuration for our specific IO transaction contains a reference to the <code>xdr</code> configuration we created above and specifies that there will not be a need to process the response as XML (we're getting JSON instead):</p>
216
217<textarea name="code" class="JScript" cols="60" rows="1">var cfg = {
218    method: "GET",
219    xdr: {
220        use:'flash', //This is the xdrConfig id we referenced above.
221        responseXML:false //we're using JSON -- marginally faster, and
222                          //supported by the Pipes API
223    },
224    on: {
225        //Our event handlers previously defined:
226        start: handleStart,
227        success: handleSuccess,
228        failure: handleFailure
229    }
230};</textarea>
231
232<p>The final step is to make the request:</p>
233
234<textarea name="code" class="JScript" cols="60" rows="1">var obj = Y.io(
235    //this is a specific Pipes feed, populated with cycling news:
236    "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
237    cfg
238);</textarea>
239
240<h3>Full Script</h3>
241
242<p>The full script source for this example is as follows:</p>
243
244<textarea name="code" class="JScript" cols="60" rows="1">
245YUI({base:"../../build/", timeout: 10000}).use("io", "substitute", "json-parse",
246
247        function(Y) {
248
249                //Data fetched will be displayed in a UL in the
250                //element #output:
251                var output = Y.Node.get("#output ul");
252
253                //Configure the cross-domain protocol:
254                var xdrConfig = {
255                        id:'flash', //We'll reference this id in the xdr configuration of our transaction.
256                        yid: Y.id,  //The yid provides a link from the Flash-based XDR engine
257                                                //and the YUI instance.
258                        src:'../../build/io/IO.swf' //Relative path to the .swf file from the current page.
259                };
260                Y.io.transport(xdrConfig);
261
262                //Event handler called when the transaction begins:
263                var handleStart = function(id, a) {
264                        Y.log("io:start firing.", "info", "example");
265                        output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>");
266                }
267
268                //Event handler for the success event -- use this handler to write the fetched
269                //RSS items to the page.
270                var handleSuccess = function(id, o, a) {
271
272                        //We use JSON.parse to sanitize the JSON (as opposed to simply eval'ing
273                        //it into the page):
274                        var oRSS = Y.JSON.parse(o.responseText);
275
276                        //From here, we simply access the JSON data from where it's provided
277                        //in the Yahoo! Pipes output:
278                        if (oRSS && oRSS.count) {
279
280                                var s = "<!--begin news stories fetched via Yahoo! Pipes-->",
281                                        //t in this case is our simple template; this is fed to
282                                        //Y.Lang.substitute as we loop through RSS items:
283                                        t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>";
284
285                                for (var i=0; i<oRSS.count; i++) {
286                                        s += Y.Lang.substitute(t, oRSS.value.items[i]);
287                                }
288
289                                //Output the string to the page:
290                                output.set("innerHTML", s);
291
292                        } else {
293                                //No news stories were found in the feed.
294                                var s = "<li>The RSS feed did not return any items.</li>";
295                        }
296                }
297
298                //In the event that the HTTP status returned is > 399, a
299                //failure is reported and this function is called:
300                var handleFailure = function(id, o, a) {
301                        Y.log("ERROR " + id + " " + a, "info", "example");
302                }
303
304                //With all the aparatus in place, we can now configure our
305                //io call.
306                var cfg = {
307                        method: "GET",
308                        xdr: {
309                                use:'flash', //This is the xdrConfig id we referenced above.
310                                responseXML:false //we're using JSON -- marginally faster, and
311                                                                  //supported by the Pipes API
312                        },
313                        on: {
314                                //Our event handlers previously defined:
315                                start: handleStart,
316                                success: handleSuccess,
317                                failure: handleFailure
318                        }
319                };
320
321                //Wire the buttton to a click handler to fire our request each
322                //time the button is clicked:
323                var handleClick = function(o) {
324                        Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example");
325                        var obj = Y.io(
326                                //this is a specific Pipes feed, populated with cycling news:
327                                "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
328                                cfg
329                        );
330                }
331
332                //add the clickHandler as soon as the xdr Flash module has
333                //loaded:
334                Y.on('io:xdrReady', function() {
335                        var fetch = Y.Node.get("#fetch");
336                        fetch.set("disabled", false);
337                        Y.on("click", handleClick, fetch);
338                });
339        }
340);
341</textarea>                             </div>
342                                <div class="yui-u sidebar">
343                                       
344                               
345                                        <div id="examples" class="mod box4">
346                        <div class="hd">
347                                                <h4>
348    IO Utility Examples:</h4>
349                        </div>
350                                                <div class="bd">
351                                                        <ul>
352                                                                <li><a href='../io/io-post.html'>POST Transaction</a></li><li><a href='../io/io-weather.html'>XML Transaction &mdash; Retrieving a Yahoo! Weather RSS (XML) Feed via a Server-Side Proxy</a></li><li class='selected'><a href='../io/io-xdr.html'>Cross-Domain JSON Transaction &mdash; Retrieving a News Feed from Yahoo! Pipes</a></li>                                                       </ul>
353                                                </div>
354                                        </div>
355                                       
356                                        <div class="mod box4">
357                        <div class="hd">
358                                                <h4>More IO Utility Resources:</h4>
359                        </div>
360                        <div class="bd">
361                                                <ul>
362                                                        <!-- <li><a href="http://developer.yahoo.com/yui/io/">User's Guide</a> (external)</li> -->
363                                                <li><a href="../../api/module_io.html">API Documentation</a></li>
364</ul>
365                        </div>
366                                        </div>
367                          </div>
368                </div>
369               
370                </div>
371        </div>
372
373
374<div class="yui-b toc3" id="tocWrapper">
375<!-- TABLE OF CONTENTS -->
376<div id="toc">
377       
378<ul>
379<li class="sect first">YUI 3.x Project</li><li class="item"><a title="The Yahoo! User Interface (YUI) Library, 3.x Branch, " href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site (external)</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="The YUI Library can be downloaded from SourceForge" href="http://sourceforge.net/projects/yui/">YUI 3 on Sourceforge (external)</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/3/license.html">YUI License (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI (Global Prerequisite) - Functional Examples" href="../../examples/yui/index.html">YUI (Global Prerequisite)</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Base - Functional Examples" href="../../examples/base/index.html">Base</a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="Drag &amp; Drop - Functional Examples" href="../../examples/dd/index.html">Drag &amp; Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="selected "><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Queue - Functional Examples" href="../../examples/queue/index.html">Queue</a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Grids - Functional Examples" href="../../examples/cssgrids/index.html">CSS Grids</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">The YUI Community</li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="item"><a title="The Yahoo! Group YDN-JavaScript hosts the YUI community forum" href="http://tech.groups.yahoo.com/group/ydn-javascript/">YUI Forum (external)</a></li><li class="item"><a title="The Yahoo! Group yui3 is dedicated to the 3.x branch of the Yahoo! User Interface (YUI) Library." href="http://tech.groups.yahoo.com/group/yui3/">YUI 3 Forum (external)</a></li><li class="item"><a title="YUI is used by Yahoo! and by hundreds of other sites, including many you know and love." href="/yui/poweredby/">YUI Sightings (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li><li class="sect">YUI Articles on the YUI Website</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Reporting Bugs and Making Feature Requests for YUI Components" href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests (external)</a></li><li class="item"><a title="Serve YUI source files from Yahoo! -- free, fast, and simple" href="http://developer.yahoo.com/yui/3/articles/hosting/">Serving YUI Files from Yahoo! (external)</a></li></ul>
380</div>
381</div>
382        </div><!--closes bd-->
383
384        <div id="ft">
385        <p class="first">Copyright &copy; 2008 Yahoo! Inc. All rights reserved.</p>
386        <p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> -
387            <a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> -
388            <a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> -
389            <a href="http://careers.yahoo.com/">Job Openings</a></p>
390        </div>
391</div>
392<script src="../../assets/dpSyntaxHighlighter.js"></script>
393<script language="javascript"> 
394dp.SyntaxHighlighter.HighlightAll('code'); 
395</script>
396</body>
397</html>
398<!-- delightfuture.corp.yahoo.com uncompressed/chunked Wed Aug 13 16:45:32 PDT 2008 -->
Note: See TracBrowser for help on using the repository browser.