src/cm/media/js/lib/yui/yui_3.10.3/docs/jsonp/jsonp-github.html
changeset 525 89ef5ed3c48b
equal deleted inserted replaced
524:322d0feea350 525:89ef5ed3c48b
       
     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: &quot;fc2cca...&quot;,
       
   117         login: &quot;username&quot;
       
   118         name: &quot;User&#x27;s Name&quot;,
       
   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>&quot;http://example.com/service?format=json&<em>callback=handleJSONP</em>&quot;.</p>
       
   134 <p>To use YUI 3's JSONP utility, replace the name of the callback function in the url with placeholder text &quot;{callback}&quot;.</p>
       
   135 
       
   136 <pre class="code prettyprint">&#x2F;&#x2F; BEFORE
       
   137 var url = &quot;https:&#x2F;&#x2F;api.github.com&#x2F;users&#x2F;yui?callback=handleJSONP&quot;;
       
   138 
       
   139 &#x2F;&#x2F;AFTER
       
   140 var url = &quot;https:&#x2F;&#x2F;api.github.com&#x2F;users&#x2F;yui?callback={callback}&quot;;</pre>
       
   141 
       
   142 
       
   143 <p>Then simply pass the url and the function that should handle the JSONP response object to <code>Y.jsonp(&lt;em&gt;url&lt;&#x2F;em&gt;, &lt;em&gt;handleJSONP&lt;&#x2F;em&gt;)</code>.</p>
       
   144 
       
   145 <pre class="code prettyprint">function handleJSONP(response) {
       
   146     Y.one(&quot;#out&quot;).setHTML(Y.Lang.sub(template, response.data));
       
   147 }
       
   148 
       
   149 Y.one(&quot;#demo_btn&quot;).on(&quot;click&quot;, function (e) {
       
   150     &#x2F;&#x2F; e.g. https:&#x2F;&#x2F;api.github.com&#x2F;users&#x2F;yui?callback={callback}
       
   151     var url = githubAPI + user.get(&quot;value&quot;) + &quot;?callback={callback}&quot;;
       
   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">&lt;div id=&quot;demo&quot;&gt;
       
   160     &lt;select id=&quot;github_user&quot;&gt;
       
   161         &lt;option value=&quot;yui&quot;&gt;YUI Library&lt;&#x2F;option&gt;
       
   162         &lt;option value=&quot;allenrabinovich&quot;&gt;Allen Rabinovich&lt;&#x2F;option&gt;
       
   163         &lt;option value=&quot;davglass&quot;&gt;Dav Glass&lt;&#x2F;option&gt;
       
   164         &lt;option value=&quot;derek&quot;&gt;Derek Gathright&lt;&#x2F;option&gt;
       
   165         &lt;option value=&quot;ericf&quot;&gt;Eric Ferraiuolo&lt;&#x2F;option&gt;
       
   166         &lt;option value=&quot;jenny&quot;&gt;Jenny Donnelly&lt;&#x2F;option&gt;
       
   167         &lt;option value=&quot;lsmith&quot;&gt;Luke Smith&lt;&#x2F;option&gt;
       
   168         &lt;option value=&quot;msweeney&quot;&gt;Matt Sweeney&lt;&#x2F;option&gt;
       
   169         &lt;option value=&quot;reid&quot;&gt;Reid Burke&lt;&#x2F;option&gt;
       
   170         &lt;option value=&quot;rgrove&quot;&gt;Ryan Grove&lt;&#x2F;option&gt;
       
   171         &lt;option value=&quot;sdesai&quot;&gt;Satyen Desai&lt;&#x2F;option&gt;
       
   172         &lt;option value=&quot;tripp&quot;&gt;Tripp Bridges&lt;&#x2F;option&gt;
       
   173     &lt;&#x2F;select&gt;
       
   174     &lt;input type=&quot;button&quot; id=&quot;demo_btn&quot; value=&quot;Get user info&quot;&gt;
       
   175     &lt;div id=&quot;out&quot;&gt;
       
   176     &lt;&#x2F;div&gt;
       
   177 &lt;&#x2F;div&gt;</pre>
       
   178 
       
   179 
       
   180 <h4>JavaScript</h4>
       
   181 <pre class="code prettyprint">&lt;script&gt;
       
   182 YUI().use(&quot;jsonp&quot;, &quot;node&quot;,function (Y) {
       
   183 
       
   184     var user      = Y.one(&quot;#github_user&quot;),
       
   185         githubAPI = &quot;https:&#x2F;&#x2F;api.github.com&#x2F;users&#x2F;&quot;,
       
   186         template  = &#x2F;&#x2F; assignment continued below
       
   187         
       
   188     &#x27;&lt;table&gt;&#x27; +
       
   189         &#x27;&lt;caption&gt;GitHub user &lt;code&gt;{login}&lt;&#x2F;code&gt; ({name})&lt;&#x2F;caption&gt;&#x27; +
       
   190         &#x27;&lt;thead&gt;&#x27; +
       
   191             &#x27;&lt;tr&gt;&#x27; +
       
   192                 &#x27;&lt;th&gt;Repositories&lt;&#x2F;th&gt;&#x27; +
       
   193                 &#x27;&lt;th&gt;Gists&lt;&#x2F;th&gt;&#x27; +
       
   194                 &#x27;&lt;th&gt;Followers&lt;&#x2F;th&gt;&#x27; +
       
   195                 &#x27;&lt;th&gt;Following&lt;&#x2F;th&gt;&#x27; +
       
   196             &#x27;&lt;&#x2F;tr&gt;&#x27; +
       
   197         &#x27;&lt;&#x2F;thead&gt;&#x27; +
       
   198         &#x27;&lt;tbody&gt;&#x27; +
       
   199             &#x27;&lt;tr&gt;&#x27; +
       
   200                 &#x27;&lt;td&gt;{public_repos}&lt;&#x2F;td&gt;&#x27; +
       
   201                 &#x27;&lt;td&gt;{public_gists}&lt;&#x2F;td&gt;&#x27; +
       
   202                 &#x27;&lt;td&gt;{followers}&lt;&#x2F;td&gt;&#x27; +
       
   203                 &#x27;&lt;td&gt;{following}&lt;&#x2F;td&gt;&#x27; +
       
   204             &#x27;&lt;&#x2F;tr&gt;&#x27; +
       
   205         &#x27;&lt;&#x2F;tbody&gt;&#x27; +
       
   206     &#x27;&lt;&#x2F;table&gt;&#x27;;
       
   207 
       
   208     function handleJSONP(response) {
       
   209         Y.one(&quot;#out&quot;).setHTML(Y.Lang.sub(template, response.data));
       
   210     }
       
   211 
       
   212     Y.one(&quot;#demo_btn&quot;).on(&quot;click&quot;, function (e) {
       
   213         &#x2F;&#x2F; e.g. https:&#x2F;&#x2F;api.github.com&#x2F;users&#x2F;yui?callback={callback}
       
   214         var url = githubAPI + user.get(&quot;value&quot;) + &quot;?callback={callback}&quot;;
       
   215 
       
   216         Y.jsonp(url, handleJSONP);
       
   217     });
       
   218 
       
   219 });
       
   220 &lt;&#x2F;script&gt;</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>