|
1 <!DOCTYPE html> |
|
2 <html lang="en"> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Example: Getting Cross Domain JSON Data Using Y.jsonp()</title> |
|
6 <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
7 <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
8 <link rel="stylesheet" href="../assets/css/main.css"> |
|
9 <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
10 <link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
11 <script src="../../build/yui/yui-min.js"></script> |
|
12 |
|
13 </head> |
|
14 <body> |
|
15 <!-- |
|
16 <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> |
|
17 --> |
|
18 <div id="doc"> |
|
19 <div id="hd"> |
|
20 <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
21 </div> |
|
22 |
|
23 |
|
24 <h1>Example: Getting Cross Domain JSON Data Using Y.jsonp()</h1> |
|
25 <div class="yui3-g"> |
|
26 <div class="yui3-u-3-4"> |
|
27 <div id="main"> |
|
28 <div class="content"><style scoped> |
|
29 #out { |
|
30 margin-top: 1em; |
|
31 } |
|
32 #out caption { |
|
33 color: #f80; |
|
34 font-size: 123%; |
|
35 display: table-caption; |
|
36 *display: block; |
|
37 } |
|
38 </style> |
|
39 |
|
40 |
|
41 <div class="intro"> |
|
42 <p>This example illustrates basic use of the <code>Y.jsonp( url, callback )</code> method, provided by the <code>jsonp</code> module.</p> |
|
43 |
|
44 <p>For this example, we will use <a href="http://develop.github.com/">GitHub's webservice API</a>, fetching user information about some members of the YUI team.</p> |
|
45 </div> |
|
46 |
|
47 <div class="example yui3-skin-sam"> |
|
48 <div id="demo"> |
|
49 <select id="github_user"> |
|
50 <option value="yui">YUI Library</option> |
|
51 <option value="allenrabinovich">Allen Rabinovich</option> |
|
52 <option value="davglass">Dav Glass</option> |
|
53 <option value="derek">Derek Gathright</option> |
|
54 <option value="ericf">Eric Ferraiuolo</option> |
|
55 <option value="jenny">Jenny Donnelly</option> |
|
56 <option value="lsmith">Luke Smith</option> |
|
57 <option value="msweeney">Matt Sweeney</option> |
|
58 <option value="reid">Reid Burke</option> |
|
59 <option value="rgrove">Ryan Grove</option> |
|
60 <option value="sdesai">Satyen Desai</option> |
|
61 <option value="tripp">Tripp Bridges</option> |
|
62 </select> |
|
63 <input type="button" id="demo_btn" value="Get user info"> |
|
64 <div id="out"> |
|
65 </div> |
|
66 </div> |
|
67 |
|
68 <script> |
|
69 YUI().use("jsonp", "node",function (Y) { |
|
70 |
|
71 var user = Y.one("#github_user"), |
|
72 githubAPI = "https://api.github.com/users/", |
|
73 template = // assignment continued below |
|
74 |
|
75 '<table>' + |
|
76 '<caption>GitHub user <code>{login}</code> ({name})</caption>' + |
|
77 '<thead>' + |
|
78 '<tr>' + |
|
79 '<th>Repositories</th>' + |
|
80 '<th>Gists</th>' + |
|
81 '<th>Followers</th>' + |
|
82 '<th>Following</th>' + |
|
83 '</tr>' + |
|
84 '</thead>' + |
|
85 '<tbody>' + |
|
86 '<tr>' + |
|
87 '<td>{public_repos}</td>' + |
|
88 '<td>{public_gists}</td>' + |
|
89 '<td>{followers}</td>' + |
|
90 '<td>{following}</td>' + |
|
91 '</tr>' + |
|
92 '</tbody>' + |
|
93 '</table>'; |
|
94 |
|
95 function handleJSONP(response) { |
|
96 Y.one("#out").setHTML(Y.Lang.sub(template, response.data)); |
|
97 } |
|
98 |
|
99 Y.one("#demo_btn").on("click", function (e) { |
|
100 // e.g. https://api.github.com/users/yui?callback={callback} |
|
101 var url = githubAPI + user.get("value") + "?callback={callback}"; |
|
102 |
|
103 Y.jsonp(url, handleJSONP); |
|
104 }); |
|
105 |
|
106 }); |
|
107 </script> |
|
108 |
|
109 </div> |
|
110 |
|
111 <h3>The data</h3> |
|
112 <p>The structure of the JavaScript object returned from GitHub's JSONP API for user info will look like this:</p> |
|
113 |
|
114 <pre class="code prettyprint">{ |
|
115 data: { |
|
116 avatar_id: "fc2cca...", |
|
117 login: "username" |
|
118 name: "User's Name", |
|
119 ..., |
|
120 |
|
121 public_repos: 10, |
|
122 public_gists: 20, |
|
123 following: 30, |
|
124 followers: 40 |
|
125 } |
|
126 }</pre> |
|
127 |
|
128 |
|
129 <p>We'll use these objects to populate HTML templates with data <em>{placeholder}</em>s using <code>Y.Lang.sub( template, object )</code>. The resulting HTML can then simply be passed to any of YUI 3's DOM creation methods, such as <code>node.setHTML( html )</code> or <code>node.append( html )</code>. We'll do this in the function that will receive the JSONP response (seen below).</p> |
|
130 |
|
131 <h3>Format the JSONP url</h3> |
|
132 <p>Typical JSONP urls are formatted like</p> |
|
133 <p>"http://example.com/service?format=json&<em>callback=handleJSONP</em>".</p> |
|
134 <p>To use YUI 3's JSONP utility, replace the name of the callback function in the url with placeholder text "{callback}".</p> |
|
135 |
|
136 <pre class="code prettyprint">// BEFORE |
|
137 var url = "https://api.github.com/users/yui?callback=handleJSONP"; |
|
138 |
|
139 //AFTER |
|
140 var url = "https://api.github.com/users/yui?callback={callback}";</pre> |
|
141 |
|
142 |
|
143 <p>Then simply pass the url and the function that should handle the JSONP response object to <code>Y.jsonp(<em>url</em>, <em>handleJSONP</em>)</code>.</p> |
|
144 |
|
145 <pre class="code prettyprint">function handleJSONP(response) { |
|
146 Y.one("#out").setHTML(Y.Lang.sub(template, response.data)); |
|
147 } |
|
148 |
|
149 Y.one("#demo_btn").on("click", function (e) { |
|
150 // e.g. https://api.github.com/users/yui?callback={callback} |
|
151 var url = githubAPI + user.get("value") + "?callback={callback}"; |
|
152 |
|
153 Y.jsonp(url, handleJSONP); |
|
154 });</pre> |
|
155 |
|
156 |
|
157 <h3 id="fullcode">Full Code Listing</h3> |
|
158 <h4>HTML</h4> |
|
159 <pre class="code prettyprint"><div id="demo"> |
|
160 <select id="github_user"> |
|
161 <option value="yui">YUI Library</option> |
|
162 <option value="allenrabinovich">Allen Rabinovich</option> |
|
163 <option value="davglass">Dav Glass</option> |
|
164 <option value="derek">Derek Gathright</option> |
|
165 <option value="ericf">Eric Ferraiuolo</option> |
|
166 <option value="jenny">Jenny Donnelly</option> |
|
167 <option value="lsmith">Luke Smith</option> |
|
168 <option value="msweeney">Matt Sweeney</option> |
|
169 <option value="reid">Reid Burke</option> |
|
170 <option value="rgrove">Ryan Grove</option> |
|
171 <option value="sdesai">Satyen Desai</option> |
|
172 <option value="tripp">Tripp Bridges</option> |
|
173 </select> |
|
174 <input type="button" id="demo_btn" value="Get user info"> |
|
175 <div id="out"> |
|
176 </div> |
|
177 </div></pre> |
|
178 |
|
179 |
|
180 <h4>JavaScript</h4> |
|
181 <pre class="code prettyprint"><script> |
|
182 YUI().use("jsonp", "node",function (Y) { |
|
183 |
|
184 var user = Y.one("#github_user"), |
|
185 githubAPI = "https://api.github.com/users/", |
|
186 template = // assignment continued below |
|
187 |
|
188 '<table>' + |
|
189 '<caption>GitHub user <code>{login}</code> ({name})</caption>' + |
|
190 '<thead>' + |
|
191 '<tr>' + |
|
192 '<th>Repositories</th>' + |
|
193 '<th>Gists</th>' + |
|
194 '<th>Followers</th>' + |
|
195 '<th>Following</th>' + |
|
196 '</tr>' + |
|
197 '</thead>' + |
|
198 '<tbody>' + |
|
199 '<tr>' + |
|
200 '<td>{public_repos}</td>' + |
|
201 '<td>{public_gists}</td>' + |
|
202 '<td>{followers}</td>' + |
|
203 '<td>{following}</td>' + |
|
204 '</tr>' + |
|
205 '</tbody>' + |
|
206 '</table>'; |
|
207 |
|
208 function handleJSONP(response) { |
|
209 Y.one("#out").setHTML(Y.Lang.sub(template, response.data)); |
|
210 } |
|
211 |
|
212 Y.one("#demo_btn").on("click", function (e) { |
|
213 // e.g. https://api.github.com/users/yui?callback={callback} |
|
214 var url = githubAPI + user.get("value") + "?callback={callback}"; |
|
215 |
|
216 Y.jsonp(url, handleJSONP); |
|
217 }); |
|
218 |
|
219 }); |
|
220 </script></pre> |
|
221 |
|
222 </div> |
|
223 </div> |
|
224 </div> |
|
225 |
|
226 <div class="yui3-u-1-4"> |
|
227 <div class="sidebar"> |
|
228 |
|
229 |
|
230 |
|
231 <div class="sidebox"> |
|
232 <div class="hd"> |
|
233 <h2 class="no-toc">Examples</h2> |
|
234 </div> |
|
235 |
|
236 <div class="bd"> |
|
237 <ul class="examples"> |
|
238 |
|
239 |
|
240 <li data-description="Get basic GitHub user info using a Y.jsonp(url, callback)."> |
|
241 <a href="jsonp-github.html">Getting Cross Domain JSON Data Using Y.jsonp()</a> |
|
242 </li> |
|
243 |
|
244 |
|
245 |
|
246 <li data-description="Create a reusable JSONPRequest object to poll the YUILibrary.com Gallery web service, fetching info on a random Gallery module."> |
|
247 <a href="jsonp-gallery.html">Reusing a JSONPRequest Instance to Poll a Remote Server</a> |
|
248 </li> |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 </ul> |
|
256 </div> |
|
257 </div> |
|
258 |
|
259 |
|
260 |
|
261 <div class="sidebox"> |
|
262 <div class="hd"> |
|
263 <h2 class="no-toc">Examples That Use This Component</h2> |
|
264 </div> |
|
265 |
|
266 <div class="bd"> |
|
267 <ul class="examples"> |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 <li data-description="Wrapping async transactions with promises"> |
|
275 <a href="../promise/basic-example.html">Wrapping async transactions with promises</a> |
|
276 </li> |
|
277 |
|
278 |
|
279 |
|
280 <li data-description="Extend Y.Promise to create classes that encapsulate standard transaction logic in descriptive method names"> |
|
281 <a href="../promise/subclass-example.html">Subclassing Y.Promise</a> |
|
282 </li> |
|
283 |
|
284 |
|
285 </ul> |
|
286 </div> |
|
287 </div> |
|
288 |
|
289 </div> |
|
290 </div> |
|
291 </div> |
|
292 </div> |
|
293 |
|
294 <script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
295 <script>prettyPrint();</script> |
|
296 |
|
297 <script> |
|
298 YUI.Env.Tests = { |
|
299 examples: [], |
|
300 project: '../assets', |
|
301 assets: '../assets/jsonp', |
|
302 name: 'jsonp-github', |
|
303 title: 'Getting Cross Domain JSON Data Using Y.jsonp()', |
|
304 newWindow: '', |
|
305 auto: false |
|
306 }; |
|
307 YUI.Env.Tests.examples.push('jsonp-github'); |
|
308 YUI.Env.Tests.examples.push('jsonp-gallery'); |
|
309 YUI.Env.Tests.examples.push('basic-example'); |
|
310 YUI.Env.Tests.examples.push('subclass-example'); |
|
311 |
|
312 </script> |
|
313 <script src="../assets/yui/test-runner.js"></script> |
|
314 |
|
315 |
|
316 |
|
317 </body> |
|
318 </html> |