~bcsaller/juju-gui/update-reductions

« back to all changes in this revision

Viewing changes to lib/yui/docs/io/xdr.html

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html>
2
 
<html lang="en">
3
 
<head>
4
 
    <meta charset="utf-8">
5
 
    <title>Example: Request JSON using Yahoo! Pipes</title>
6
 
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
 
    <link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
 
    <link rel="stylesheet" href="../assets/css/main.css">
9
 
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
 
    <script src="../../build/yui/yui-min.js"></script>
11
 
</head>
12
 
<body>
13
 
 
14
 
<div id="doc">
15
 
    <h1>Example: Request JSON using Yahoo! Pipes</h1>
16
 
 
17
 
    
18
 
 
19
 
    <div class="yui3-g">
20
 
        <div class="yui3-u-3-4">
21
 
            <div id="main">
22
 
                <div class="content"><style type="text/css" scoped>
23
 
#output li {margin-left:2em;}
24
 
#output { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-top:1em;}
25
 
</style>
26
 
<div class="intro">
27
 
<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>
28
 
<p>This example demonstrates how IO can use the Cross-Origin Resource Sharing (http://www.w3.org/TR/cors/) mechanism for making cross-domain requests.</p>
29
 
<p><strong>Please note: </strong> All the browsers listed in the Browser Test Baseline (http://developer.yahoo.com/yui/articles/gbs/) support CORS with the exception of IE 6.0 and IE 7.0, and Webkit on iOS 3.  In addition to browser capability, the requested resource must respond with the proper Access-Control headers for the request to succeed.</p>
30
 
</div>
31
 
<div class="example">
32
 
<button id="pipes">Load RSS news feed from Yahoo! Pipes.</button>
33
 
<div id="output">
34
 
    <ul>
35
 
        <li>Content from Yahoo! Pipes feed will display here.</li>
36
 
    </ul>
37
 
</div>
38
 
 
39
 
<script language="javascript">
40
 
YUI({ filter:'raw' }).use("io-xdr", "substitute", "json-parse", "node",
41
 
    function(Y) {
42
 
 
43
 
        //Data fetched will be displayed in a UL in the
44
 
        //element #output:
45
 
        var output = Y.one("#output ul");
46
 
 
47
 
        //Event handler called when the transaction begins:
48
 
        var handleStart = function(id, a) {
49
 
            Y.log("io:start firing.", "info", "example");
50
 
            output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>");
51
 
        }
52
 
 
53
 
        //Event handler for the success event -- use this handler to write the fetched
54
 
        //RSS items to the page.
55
 
        var handleSuccess = function(id, o, a) {
56
 
 
57
 
            //We use JSON.parse to sanitize the JSON (as opposed to simply performing an
58
 
            //JavaScript eval of the data):
59
 
            var oRSS = Y.JSON.parse(o.responseText);
60
 
 
61
 
            //From here, we simply access the JSON data from where it's provided
62
 
            //in the Yahoo! Pipes output:
63
 
            if (oRSS && oRSS.count) {
64
 
 
65
 
                var s = "<!--begin news stories fetched via Yahoo! Pipes-->",
66
 
                    //t in this case is our simple template; this is fed to
67
 
                    //Y.Lang.substitute as we loop through RSS items:
68
 
                    t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>";
69
 
 
70
 
                for (var i=0; i<oRSS.count; i++) {
71
 
                    s += Y.Lang.substitute(t, oRSS.value.items[i]);
72
 
                }
73
 
 
74
 
                //Output the string to the page:
75
 
                output.set("innerHTML", s);
76
 
                output.addClass("yui-null");
77
 
 
78
 
            } else {
79
 
                //No news stories were found in the feed.
80
 
                var s = "<li>The RSS feed did not return any items.</li>";
81
 
            }
82
 
        }
83
 
 
84
 
        //In the event that the HTTP status returned does not resolve to,
85
 
        //HTTP 2xx, a failure is reported and this function is called:
86
 
        var handleFailure = function(id, o, a) {
87
 
            Y.log("ERROR " + id + " " + a, "info", "example");
88
 
        }
89
 
 
90
 
        //With all the apparatus in place, we can now configure our
91
 
        //IO call.  The method property is defined, but if omitted,
92
 
        //IO will default to HTTP GET.
93
 
        var cfg = {
94
 
            method: "GET",
95
 
            xdr: {
96
 
                use:'native'
97
 
            },
98
 
            on: {
99
 
                //Our event handlers previously defined:
100
 
                start: handleStart,
101
 
                success: handleSuccess,
102
 
                failure: handleFailure
103
 
            }
104
 
        };
105
 
 
106
 
        //Wire the button to a click handler to fire our request each
107
 
        //time the button is clicked:
108
 
        var handleClick = function(o) {
109
 
            Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example");
110
 
            // Remove the default "X-Requested-With" header as this will
111
 
            // prevent the request from succeeding; the Pipes 
112
 
            // resource will not accept user-defined HTTP headers.
113
 
            Y.io.header('X-Requested-With');
114
 
            var obj = Y.io(
115
 
                //this is a specific Pipes feed, populated with cycling news:
116
 
                "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
117
 
                cfg
118
 
            );
119
 
        }
120
 
 
121
 
        //add the click handler to the Load button.
122
 
        Y.on("click", handleClick, "#pipes");
123
 
    }
124
 
);
125
 
</script>
126
 
</div>
127
 
 
128
 
<h3 class="first">Implementing a Cross-Domain Request for JSON Data</h3>
129
 
 
130
 
<p>In this example, we begin with a YUI instance that loads the <code>io-xdr</code>, <code>json-parse</code>, <code>substitute</code> and <code>node</code> modules. The <code>io-xdr</code> module is the key module. The other modules are used to process and output the results:</p>
131
 
 
132
 
<pre class="code prettyprint">&#x2F;&#x2F;Create a YUI instance including support for IO and JSON modules:
133
 
YUI().use(&quot;io-xdr&quot;, &quot;substitute&quot;, &quot;json-parse&quot;, &quot;node&quot;, function(Y) {
134
 
    &#x2F;&#x2F; Y is the YUI instance.
135
 
    &#x2F;&#x2F; The rest of the following code is encapsulated in this
136
 
    &#x2F;&#x2F; anonymous function.
137
 
});</pre>
138
 
 
139
 
 
140
 
<p>We'll also get a Node reference to the container we'll be using to output the data we retrieve:</p>
141
 
 
142
 
<pre class="code prettyprint">&#x2F;&#x2F;element #output:
143
 
var output = Y.one(&quot;#output ul&quot;);</pre>
144
 
 
145
 
 
146
 
<p>handleSuccess is the function responsible for handling the response data.  The first thing we do is sanitize the data to ensure we have valid JSON.</p>
147
 
<pre class="code prettyprint">var oRSS = Y.JSON.parse(o.responseText);</pre>
148
 
 
149
 
<p>Next, we create a simple markup template and use Y.Lang.substitute to fill in the data, as we iterate through the JSON and output the results.</p>
150
 
<pre class="code prettyprint">&#x2F;&#x2F;From here, we simply access the JSON data from where it&#x27;s provided
151
 
&#x2F;&#x2F;in the Yahoo! Pipes output:
152
 
if (oRSS &amp;&amp; oRSS.count) {
153
 
    var s = &quot;&lt;!--begin news stories fetched via Yahoo! Pipes--&gt;&quot;,
154
 
        &#x2F;&#x2F;t in this case is our simple template; this is fed to
155
 
        &#x2F;&#x2F;Y.Lang.substitute as we loop through RSS items:
156
 
        t = &quot;&lt;li&gt;&lt;a href=&#x27;{link}&#x27;&gt;{title}&lt;&#x2F;a&gt;, {pubDate}&lt;&#x2F;li&gt;&quot;;
157
 
 
158
 
    for (var i=0; i&lt;oRSS.count; i++) {
159
 
        s += Y.Lang.substitute(t, oRSS.value.items[i]);
160
 
    }
161
 
 
162
 
    &#x2F;&#x2F;Output the string to the page:
163
 
    output.set(&quot;innerHTML&quot;, s);
164
 
    output.addClass(&quot;yui-null&quot;);
165
 
}</pre>
166
 
 
167
 
 
168
 
<p>Create the configuration object for the cross-domain request, setting up the  event handlers and instructing IO to use the browser's native cross-domain transport.</p>
169
 
 
170
 
<pre class="code prettyprint">var cfg = {
171
 
    method: &quot;GET&quot;, &#x2F;&#x2F;If omitted, the default is HTTP GET.
172
 
    xdr: {
173
 
        use:&#x27;native&#x27;&#x2F;&#x2F;For browsers that support CORS.
174
 
    },
175
 
    on: {
176
 
        &#x2F;&#x2F;Our event handlers previously defined:
177
 
        start: handleStart,
178
 
        success: handleSuccess,
179
 
        failure: handleFailure
180
 
    }
181
 
};</pre>
182
 
 
183
 
 
184
 
<p>Create an event handler that will make the IO call to Yahoo! Pipes when the Load button is clicked:</p>
185
 
 
186
 
<pre class="code prettyprint">&#x2F;&#x2F;Wire the button to a click handler to fire our request each
187
 
&#x2F;&#x2F;time the button is clicked:
188
 
var handleClick = function(o) {
189
 
    Y.log(&quot;Click detected; beginning io request to Yahoo! Pipes.&quot;, &quot;info&quot;, &quot;example&quot;);
190
 
    &#x2F;&#x2F; Remove the default &quot;X-Requested-With&quot; header as this will
191
 
    &#x2F;&#x2F; prevent the request from succeeding; the Pipes 
192
 
    &#x2F;&#x2F; resource will not accept user-defined HTTP headers.
193
 
    Y.io.header(&#x27;X-Requested-With&#x27;);
194
 
    var obj = Y.io(
195
 
        &#x2F;&#x2F;this is a specific Pipes feed, populated with cycling news:
196
 
        &quot;http:&#x2F;&#x2F;pipes.yahooapis.com&#x2F;pipes&#x2F;pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&amp;_render=json&quot;,
197
 
        cfg
198
 
    );
199
 
}
200
 
 
201
 
&#x2F;&#x2F;add the click handler to the Load button.
202
 
Y.on(&quot;click&quot;, handleClick, &quot;#pipes&quot;);</pre>
203
 
 
204
 
 
205
 
<h4>Full Script</h4>
206
 
 
207
 
<p>The full script source for this example is as follows:</p>
208
 
 
209
 
<pre class="code prettyprint">&lt;button id=&quot;pipes&quot;&gt;Load RSS news feed from Yahoo! Pipes.&lt;&#x2F;button&gt;
210
 
&lt;div id=&quot;output&quot;&gt;
211
 
    &lt;ul&gt;
212
 
        &lt;li&gt;Content from Yahoo! Pipes feed will display here.&lt;&#x2F;li&gt;
213
 
    &lt;&#x2F;ul&gt;
214
 
&lt;&#x2F;div&gt;
215
 
 
216
 
&lt;script language=&quot;javascript&quot;&gt;
217
 
YUI({ filter:&#x27;raw&#x27; }).use(&quot;io-xdr&quot;, &quot;substitute&quot;, &quot;json-parse&quot;, &quot;node&quot;,
218
 
    function(Y) {
219
 
 
220
 
        &#x2F;&#x2F;Data fetched will be displayed in a UL in the
221
 
        &#x2F;&#x2F;element #output:
222
 
        var output = Y.one(&quot;#output ul&quot;);
223
 
 
224
 
        &#x2F;&#x2F;Event handler called when the transaction begins:
225
 
        var handleStart = function(id, a) {
226
 
            Y.log(&quot;io:start firing.&quot;, &quot;info&quot;, &quot;example&quot;);
227
 
            output.set(&quot;innerHTML&quot;, &quot;&lt;li&gt;Loading news stories via Yahoo! Pipes feed...&lt;&#x2F;li&gt;&quot;);
228
 
        }
229
 
 
230
 
        &#x2F;&#x2F;Event handler for the success event -- use this handler to write the fetched
231
 
        &#x2F;&#x2F;RSS items to the page.
232
 
        var handleSuccess = function(id, o, a) {
233
 
 
234
 
            &#x2F;&#x2F;We use JSON.parse to sanitize the JSON (as opposed to simply performing an
235
 
            &#x2F;&#x2F;JavaScript eval of the data):
236
 
            var oRSS = Y.JSON.parse(o.responseText);
237
 
 
238
 
            &#x2F;&#x2F;From here, we simply access the JSON data from where it&#x27;s provided
239
 
            &#x2F;&#x2F;in the Yahoo! Pipes output:
240
 
            if (oRSS &amp;&amp; oRSS.count) {
241
 
 
242
 
                var s = &quot;&lt;!--begin news stories fetched via Yahoo! Pipes--&gt;&quot;,
243
 
                    &#x2F;&#x2F;t in this case is our simple template; this is fed to
244
 
                    &#x2F;&#x2F;Y.Lang.substitute as we loop through RSS items:
245
 
                    t = &quot;&lt;li&gt;&lt;a href=&#x27;{link}&#x27;&gt;{title}&lt;&#x2F;a&gt;, {pubDate}&lt;&#x2F;li&gt;&quot;;
246
 
 
247
 
                for (var i=0; i&lt;oRSS.count; i++) {
248
 
                    s += Y.Lang.substitute(t, oRSS.value.items[i]);
249
 
                }
250
 
 
251
 
                &#x2F;&#x2F;Output the string to the page:
252
 
                output.set(&quot;innerHTML&quot;, s);
253
 
                output.addClass(&quot;yui-null&quot;);
254
 
 
255
 
            } else {
256
 
                &#x2F;&#x2F;No news stories were found in the feed.
257
 
                var s = &quot;&lt;li&gt;The RSS feed did not return any items.&lt;&#x2F;li&gt;&quot;;
258
 
            }
259
 
        }
260
 
 
261
 
        &#x2F;&#x2F;In the event that the HTTP status returned does not resolve to,
262
 
        &#x2F;&#x2F;HTTP 2xx, a failure is reported and this function is called:
263
 
        var handleFailure = function(id, o, a) {
264
 
            Y.log(&quot;ERROR &quot; + id + &quot; &quot; + a, &quot;info&quot;, &quot;example&quot;);
265
 
        }
266
 
 
267
 
        &#x2F;&#x2F;With all the apparatus in place, we can now configure our
268
 
        &#x2F;&#x2F;IO call.  The method property is defined, but if omitted,
269
 
        &#x2F;&#x2F;IO will default to HTTP GET.
270
 
        var cfg = {
271
 
            method: &quot;GET&quot;,
272
 
            xdr: {
273
 
                use:&#x27;native&#x27;
274
 
            },
275
 
            on: {
276
 
                &#x2F;&#x2F;Our event handlers previously defined:
277
 
                start: handleStart,
278
 
                success: handleSuccess,
279
 
                failure: handleFailure
280
 
            }
281
 
        };
282
 
 
283
 
        &#x2F;&#x2F;Wire the button to a click handler to fire our request each
284
 
        &#x2F;&#x2F;time the button is clicked:
285
 
        var handleClick = function(o) {
286
 
            Y.log(&quot;Click detected; beginning io request to Yahoo! Pipes.&quot;, &quot;info&quot;, &quot;example&quot;);
287
 
            &#x2F;&#x2F; Remove the default &quot;X-Requested-With&quot; header as this will
288
 
            &#x2F;&#x2F; prevent the request from succeeding; the Pipes 
289
 
            &#x2F;&#x2F; resource will not accept user-defined HTTP headers.
290
 
            Y.io.header(&#x27;X-Requested-With&#x27;);
291
 
            var obj = Y.io(
292
 
                &#x2F;&#x2F;this is a specific Pipes feed, populated with cycling news:
293
 
                &quot;http:&#x2F;&#x2F;pipes.yahooapis.com&#x2F;pipes&#x2F;pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&amp;_render=json&quot;,
294
 
                cfg
295
 
            );
296
 
        }
297
 
 
298
 
        &#x2F;&#x2F;add the click handler to the Load button.
299
 
        Y.on(&quot;click&quot;, handleClick, &quot;#pipes&quot;);
300
 
    }
301
 
);
302
 
&lt;&#x2F;script&gt;</pre>
303
 
 
304
 
</div>
305
 
            </div>
306
 
        </div>
307
 
 
308
 
        <div class="yui3-u-1-4">
309
 
            <div class="sidebar">
310
 
                
311
 
 
312
 
                
313
 
                    <div class="sidebox">
314
 
                        <div class="hd">
315
 
                            <h2 class="no-toc">Examples</h2>
316
 
                        </div>
317
 
 
318
 
                        <div class="bd">
319
 
                            <ul class="examples">
320
 
                                
321
 
                                    
322
 
                                        <li data-description="Use IO to request data over HTTP.">
323
 
                                            <a href="get.html">HTTP GET to request data</a>
324
 
                                        </li>
325
 
                                    
326
 
                                
327
 
                                    
328
 
                                        <li data-description="Use IO to request XML data from a remote web service.">
329
 
                                            <a href="weather.html">Request XML data from Yahoo! Weather</a>
330
 
                                        </li>
331
 
                                    
332
 
                                
333
 
                                    
334
 
                                        <li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
335
 
                                            <a href="xdr.html">Request JSON using Yahoo! Pipes</a>
336
 
                                        </li>
337
 
                                    
338
 
                                
339
 
                                    
340
 
                                
341
 
                            </ul>
342
 
                        </div>
343
 
                    </div>
344
 
                
345
 
 
346
 
                
347
 
                    <div class="sidebox">
348
 
                        <div class="hd">
349
 
                            <h2 class="no-toc">Examples That Use This Component</h2>
350
 
                        </div>
351
 
 
352
 
                        <div class="bd">
353
 
                            <ul class="examples">
354
 
                                
355
 
                                    
356
 
                                
357
 
                                    
358
 
                                
359
 
                                    
360
 
                                
361
 
                                    
362
 
                                        <li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
363
 
                                            <a href="../overlay/overlay-io-plugin.html">IO Plugin</a>
364
 
                                        </li>
365
 
                                    
366
 
                                
367
 
                            </ul>
368
 
                        </div>
369
 
                    </div>
370
 
                
371
 
            </div>
372
 
        </div>
373
 
    </div>
374
 
</div>
375
 
 
376
 
<script src="../assets/vendor/prettify/prettify-min.js"></script>
377
 
<script>prettyPrint();</script>
378
 
 
379
 
</body>
380
 
</html>