|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>Cross-Domain JSON Transaction — Retrieving a News Feed from Yahoo! Pipes</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
#output li {margin-left:2em;} |
|
|
27 |
#output { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-top:1em;} |
|
|
28 |
</style> |
|
|
29 |
<!--end custom header content for this example--> |
|
|
30 |
|
|
|
31 |
</head> |
|
|
32 |
|
|
|
33 |
<body class=" yui-skin-sam"> |
|
|
34 |
|
|
|
35 |
<h1>Cross-Domain JSON Transaction — Retrieving a News Feed from Yahoo! Pipes</h1> |
|
|
36 |
|
|
|
37 |
<div class="exampleIntro"> |
|
|
38 |
<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> |
|
|
39 |
|
|
|
40 |
<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> |
|
|
41 |
</div> |
|
|
42 |
|
|
|
43 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
44 |
|
|
|
45 |
<button id="fetch" disabled="disabled">Load JSON RSS news feed from Yahoo! Pipes.</button> |
|
|
46 |
|
|
|
47 |
<div id="output"> |
|
|
48 |
<ul> |
|
|
49 |
<li>Content from Yahoo! Pipes feed will display here.</li> |
|
|
50 |
</ul> |
|
|
51 |
</div> |
|
|
52 |
|
|
|
53 |
<script language="javascript"> |
|
|
54 |
|
|
|
55 |
YUI({base:"../../build/", timeout: 10000}).use("io", "substitute", "json-parse", |
|
|
56 |
|
|
|
57 |
function(Y) { |
|
|
58 |
|
|
|
59 |
//Data fetched will be displayed in a UL in the |
|
|
60 |
//element #output: |
|
|
61 |
var output = Y.Node.get("#output ul"); |
|
|
62 |
|
|
|
63 |
//Configure the cross-domain protocol: |
|
|
64 |
var xdrConfig = { |
|
|
65 |
id:'flash', //We'll reference this id in the xdr configuration of our transaction. |
|
|
66 |
yid: Y.id, //The yid provides a link from the Flash-based XDR engine |
|
|
67 |
//and the YUI instance. |
|
|
68 |
src:'../../build/io/io.swf?t=' + new Date().valueOf().toString() //Relative path to the .swf file from the current page. |
|
|
69 |
}; |
|
|
70 |
Y.io.transport(xdrConfig); |
|
|
71 |
|
|
|
72 |
//Event handler called when the transaction begins: |
|
|
73 |
var handleStart = function(id, a) { |
|
|
74 |
Y.log("io:start firing.", "info", "example"); |
|
|
75 |
output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>"); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
//Event handler for the success event -- use this handler to write the fetched |
|
|
79 |
//RSS items to the page. |
|
|
80 |
var handleSuccess = function(id, o, a) { |
|
|
81 |
|
|
|
82 |
//We use JSON.parse to sanitize the JSON (as opposed to simply eval'ing |
|
|
83 |
//it into the page): |
|
|
84 |
var oRSS = Y.JSON.parse(o.responseText); |
|
|
85 |
|
|
|
86 |
//From here, we simply access the JSON data from where it's provided |
|
|
87 |
//in the Yahoo! Pipes output: |
|
|
88 |
if (oRSS && oRSS.count) { |
|
|
89 |
|
|
|
90 |
var s = "<!--begin news stories fetched via Yahoo! Pipes-->", |
|
|
91 |
//t in this case is our simple template; this is fed to |
|
|
92 |
//Y.Lang.substitute as we loop through RSS items: |
|
|
93 |
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>"; |
|
|
94 |
|
|
|
95 |
for (var i=0; i<oRSS.count; i++) { |
|
|
96 |
s += Y.Lang.substitute(t, oRSS.value.items[i]); |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
//Output the string to the page: |
|
|
100 |
output.set("innerHTML", s); |
|
|
101 |
output.addClass("yui-null"); |
|
|
102 |
|
|
|
103 |
} else { |
|
|
104 |
//No news stories were found in the feed. |
|
|
105 |
var s = "<li>The RSS feed did not return any items.</li>"; |
|
|
106 |
} |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
//In the event that the HTTP status returned is > 399, a |
|
|
110 |
//failure is reported and this function is called: |
|
|
111 |
var handleFailure = function(id, o, a) { |
|
|
112 |
Y.log("ERROR " + id + " " + a, "info", "example"); |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
//With all the aparatus in place, we can now configure our |
|
|
116 |
//io call. |
|
|
117 |
var cfg = { |
|
|
118 |
method: "GET", |
|
|
119 |
xdr: { |
|
|
120 |
use:'flash' //This is the xdrConfig id we referenced above. |
|
|
121 |
}, |
|
|
122 |
on: { |
|
|
123 |
//Our event handlers previously defined: |
|
|
124 |
start: handleStart, |
|
|
125 |
success: handleSuccess, |
|
|
126 |
failure: handleFailure |
|
|
127 |
} |
|
|
128 |
}; |
|
|
129 |
|
|
|
130 |
//Wire the buttton to a click handler to fire our request each |
|
|
131 |
//time the button is clicked: |
|
|
132 |
var handleClick = function(o) { |
|
|
133 |
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example"); |
|
|
134 |
var obj = Y.io( |
|
|
135 |
//this is a specific Pipes feed, populated with cycling news: |
|
|
136 |
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json", |
|
|
137 |
cfg |
|
|
138 |
); |
|
|
139 |
} |
|
|
140 |
|
|
|
141 |
//add the clickHandler as soon as the xdr Flash module has |
|
|
142 |
//loaded: |
|
|
143 |
Y.on('io:xdrReady', function() { |
|
|
144 |
var fetch = Y.Node.get("#fetch"); |
|
|
145 |
fetch.set("disabled", false); |
|
|
146 |
Y.on("click", handleClick, fetch); |
|
|
147 |
}); |
|
|
148 |
|
|
|
149 |
} |
|
|
150 |
); |
|
|
151 |
</script> |
|
|
152 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
153 |
|
|
|
154 |
</body> |
|
|
155 |
</html> |