src/cm/media/js/lib/yui/yui_3.10.3/docs/json/json-convert-values.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: Adding New Object Members During Parsing</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: Adding New Object Members During Parsing</h1>
       
    25     <div class="yui3-g">
       
    26         <div class="yui3-u-3-4">
       
    27             <div id="main">
       
    28                 <div class="content"><div class="intro">
       
    29 <p>
       
    30     This example shows how to use the <code>reviver</code> parameter in <code>JSON.parse()</code> to add new object members and format existing members during the parsing phase.
       
    31 </p>
       
    32 </div>
       
    33 
       
    34 <div class="example yui3-skin-sam">
       
    35     <div id="demo">
       
    36     <p>Choose a currency, then get the data</p>
       
    37     <select>
       
    38         <option value="ARS">Argentine Peso</option>
       
    39         <option value="AUD">Australian Dollar</option>
       
    40         <option value="BRL">Brazilian Real</option>
       
    41         <option value="GBP">British Pound</option>
       
    42         <option value="CAD">Canadian Dollar</option>
       
    43         <option value="CNY">Chinese Yuan</option>
       
    44         <option value="COP">Colombian Peso</option>
       
    45         <option value="HRK">Croatian Kuna</option>
       
    46         <option value="CZK">Czech Koruna</option>
       
    47         <option value="DKK">Danish Krone</option>
       
    48         <option value="EEK">Estonian Kroon</option>
       
    49         <option value="EUR">Euro</option>
       
    50         <option value="HKD">Hong Kong Dollar</option>
       
    51         <option value="HUF">Hungarian Forint</option>
       
    52         <option value="ISK">Iceland Krona</option>
       
    53         <option value="INR">Indian Rupee</option>
       
    54         <option value="JPY">Japanese Yen</option>
       
    55         <option value="KRW">Korean Won</option>
       
    56         <option value="LVL">Latvian Lat</option>
       
    57         <option value="LTL">Lithuanian Lita</option>
       
    58         <option value="MYR">Malaysian Ringgit</option>
       
    59         <option value="MXN">Mexican Peso</option>
       
    60         <option value="NZD">New Zealand Dollar</option>
       
    61         <option value="NOK">Norwegian Krone</option>
       
    62         <option value="PHP">Philippine Peso</option>
       
    63         <option value="PLN">Polish Zloty</option>
       
    64         <option value="RUB">Russian Rouble</option>
       
    65         <option value="SGD">Singapore Dollar</option>
       
    66         <option value="SKK">Slovak Koruna</option>
       
    67         <option value="ZAR">South African Rand</option>
       
    68         <option value="LKR">Sri Lanka Rupee</option>
       
    69         <option value="SEK">Swedish Krona</option>
       
    70         <option value="TRY">Turkey Lira</option>
       
    71         <option value="USD" selected="selected">U.S. Dollar</option>
       
    72         <option value="CHF">Swiss Franc</option>
       
    73         <option value="TWD">Taiwan Dollar</option>
       
    74         <option value="THB">Thai Baht</option>
       
    75     </select>
       
    76     <input type="button" id="demo_go" value="Get Data">
       
    77 
       
    78     <table cellspacing="0">
       
    79     <caption>Inventory</caption>
       
    80     <thead>
       
    81         <tr>
       
    82             <th>SKU</th>
       
    83             <th>Item</th>
       
    84             <th>Price (USD)</th>
       
    85             <th>Price (<span>USD</span>)</th>
       
    86         </tr>
       
    87     </thead>
       
    88     <tbody>
       
    89         <tr><td colspan="4">Click <em>Get Data</em></td></tr>
       
    90     </tbody>
       
    91     </table>
       
    92 </div>
       
    93 
       
    94 <script>
       
    95 YUI().use("node", "io", "json-parse",function (Y) {
       
    96 
       
    97 // Safari 4.0.3's native JSON does not support adding members during parse,
       
    98 // so use JavaScript implementation for consistency
       
    99 Y.JSON.useNativeParse = false;
       
   100 
       
   101 var example = {
       
   102     rates : {"USD":1,"EUR":0.6661,"GBP":0.5207,"AUD":1.1225,"BRL":1.609,"NZD":1.4198,"CAD":1.0667,"CHF":1.0792,"CNY":6.8587 ,"DKK":4.9702,"HKD":7.8064,"INR":42.0168,"JPY":109.8901,"KRW":1000,"LKR":107.5269,"MXN":10.1317,"MYR" :3.3167,"NOK":5.3277,"SEK":6.2617,"SGD":1.4073,"THB":33.7838,"TWD":31.1526,"VEF":2.1445,"ZAR":7.6923 ,"BGN":1.3028,"CZK":16.0514,"EEK":10.4275,"HUF":158.7302,"LTL":2.2999,"LVL":0.4692,"PLN":2.1758,"RON" :2.3804,"SKK":20.2429,"ISK":4.8008,"HRK":81.3008,"RUB":24.3309,"TRY":1.1811,"PHP":44.2478,"COP":2000 ,"ARS":3.1289},
       
   103 
       
   104     currency : 'USD',
       
   105 
       
   106     convert : function (k,v) {
       
   107         // 'this' will refer to the object containing the key:value pair,
       
   108         // so this step will add a new object member while leaving the original
       
   109         // intact (but formatted to two decimal places).  If the original
       
   110         // is not needed, just return the converted value.
       
   111         if (k === 'Price') {
       
   112             var x = Math.round(v * example.rates[example.currency] * 100) / 100;
       
   113             this.convertedPrice = x.toFixed(2); // added to item
       
   114             return v.toFixed(2); // assigned to item.Price
       
   115         }
       
   116         return v;
       
   117     },
       
   118 
       
   119     updateTable : function (inventory) {
       
   120         // Update the column header
       
   121         Y.one('#demo table th span').set('innerHTML',example.currency);
       
   122 
       
   123         var tbody = Y.one('#demo table tbody'),
       
   124             html  = ['<table><tbody>'],
       
   125             rowTemplate = '<tr><td>{SKU}</td><td>{Item}</td><td>{Price}</td><td>{convertedPrice}</td></tr>',
       
   126             i, len;
       
   127 
       
   128         if (inventory) {
       
   129             for (i = 0, len = inventory.length; i < len; ++i) {
       
   130                 html.push(Y.Lang.sub(rowTemplate, inventory[i]));
       
   131             }
       
   132         } else {
       
   133             html.push('<tr><td colspan="4">No Inventory data</td></tr>');
       
   134         }
       
   135 
       
   136         html.push('</tbody></table>');
       
   137 
       
   138         tbody.replace(Y.Node.create(html.join('')).one('tbody'));
       
   139     }
       
   140 };
       
   141 
       
   142 Y.one('#demo_go').on('click', function (e) {
       
   143     // Disable the button temporarily
       
   144     this.set('disabled',true);
       
   145 
       
   146     // Store the requested currency
       
   147     var sel = Y.one("#demo select");
       
   148     example.currency = sel.get("options").item(sel.get("selectedIndex")).get("value");
       
   149 
       
   150     Y.io('../assets/json/json-convert-values-response.json',{
       
   151         timeout : 3000,
       
   152         on : {
       
   153             success : function (xid, res) {
       
   154                 var inventory;
       
   155                 try {
       
   156                     inventory = Y.JSON.parse(res.responseText,example.convert);
       
   157 
       
   158                     example.updateTable(inventory);
       
   159                 }
       
   160                 catch(e) {
       
   161                     alert("Error getting inventory data");
       
   162                 }
       
   163                 finally {
       
   164                     Y.one('#demo_go').set('disabled',false);
       
   165                 }
       
   166             },
       
   167             failure : function () {
       
   168                 alert("Error getting inventory data");
       
   169             }
       
   170         }
       
   171     });
       
   172 });
       
   173 
       
   174 // Expose example objects for inspection
       
   175 YUI.example = example;
       
   176 });
       
   177 </script>
       
   178 
       
   179 </div>
       
   180 
       
   181 <h2>The Data</h2>
       
   182 <p>
       
   183     The data returned from the server will be a JSON string containing this object structure:
       
   184 </p>
       
   185 
       
   186 <pre class="code prettyprint lang-json">[
       
   187     {&quot;SKU&quot;:&quot;23-23874&quot;, &quot;Price&quot;:23.99,  &quot;Item&quot;:&quot;Helmet&quot;},
       
   188     {&quot;SKU&quot;:&quot;48-38835&quot;, &quot;Price&quot;:14.97,  &quot;Item&quot;:&quot;Football&quot;},
       
   189     {&quot;SKU&quot;:&quot;84-84848&quot;, &quot;Price&quot;:3.49,   &quot;Item&quot;:&quot;Goggles&quot;},
       
   190     {&quot;SKU&quot;:&quot;84-84843&quot;, &quot;Price&quot;:183,    &quot;Item&quot;:&quot;Badminton Set&quot;},
       
   191     {&quot;SKU&quot;:&quot;84-39321&quot;, &quot;Price&quot;:6.79,   &quot;Item&quot;:&quot;Tennis Balls&quot;},
       
   192     {&quot;SKU&quot;:&quot;39-48949&quot;, &quot;Price&quot;:618,    &quot;Item&quot;:&quot;Snowboard&quot;},
       
   193     {&quot;SKU&quot;:&quot;99-28128&quot;, &quot;Price&quot;:78.99,  &quot;Item&quot;:&quot;Cleats&quot;},
       
   194     {&quot;SKU&quot;:&quot;83-48281&quot;, &quot;Price&quot;:4.69,   &quot;Item&quot;:&quot;Volleyball&quot;},
       
   195     {&quot;SKU&quot;:&quot;89-32811&quot;, &quot;Price&quot;:0.59,   &quot;Item&quot;:&quot;Sweatband&quot;},
       
   196     {&quot;SKU&quot;:&quot;28-22847&quot;, &quot;Price&quot;:779.98, &quot;Item&quot;:&quot;Golf Set&quot;},
       
   197     {&quot;SKU&quot;:&quot;38-38281&quot;, &quot;Price&quot;:8.25,   &quot;Item&quot;:&quot;Basketball Shorts&quot;},
       
   198     {&quot;SKU&quot;:&quot;82-38333&quot;, &quot;Price&quot;:1.39,   &quot;Item&quot;:&quot;Lip balm&quot;},
       
   199     {&quot;SKU&quot;:&quot;21-38485&quot;, &quot;Price&quot;:0.07,   &quot;Item&quot;:&quot;Ping Pong ball&quot;},
       
   200     {&quot;SKU&quot;:&quot;83-38285&quot;, &quot;Price&quot;:3.99,   &quot;Item&quot;:&quot;Hockey Puck&quot;}
       
   201 ]</pre>
       
   202 
       
   203 
       
   204 <h2>Create a <code>reviver</code> function</h2>
       
   205 <p>
       
   206     We'll contain all the moving parts in an <code>example</code> namespace. In it, we'll include the currency exchange rates and a function to reference the rates to add a new member to the JSON response as it is being parsed.
       
   207 </p>
       
   208 
       
   209 <pre class="code prettyprint">YUI().use(&quot;node&quot;, &quot;io&quot;, &quot;json-parse&quot;,function (Y) {
       
   210 
       
   211 var example = {
       
   212     rates : {&quot;USD&quot;:1,&quot;EUR&quot;:0.6661,...,&quot;COP&quot;:2000 ,&quot;ARS&quot;:3.1289},
       
   213 
       
   214     currency : &#x27;USD&#x27;, &#x2F;&#x2F; updated by the select element
       
   215 
       
   216     convert : function (k,v) {
       
   217         &#x2F;&#x2F; &#x27;this&#x27; will refer to the object containing the key:value pair,
       
   218         &#x2F;&#x2F; so this step will add a new object member while leaving the original
       
   219         &#x2F;&#x2F; intact (but formatted to two decimal places).  If the original
       
   220         &#x2F;&#x2F; is not needed, just return the converted value.
       
   221         if (k === &#x27;Price&#x27;) {
       
   222             var x = Math.round(v * example.rates[example.currency] * 100) &#x2F; 100;
       
   223             this.convertedPrice = x.toFixed(2); &#x2F;&#x2F; added to item
       
   224             return v.toFixed(2); &#x2F;&#x2F; assigned to item.Price
       
   225         }
       
   226         return v;
       
   227     },
       
   228
       
   229 };
       
   230 …</pre>
       
   231 
       
   232 
       
   233 <h2>Sending the request and parsing the JSON response</h2>
       
   234 <p>
       
   235     When the <em>Get Data</em> button is clicked, we send an io request for the JSON data, and in our <code>success</code> handler, pass our conversion function to <code>JSON.parse()</code> with the response text. The resulting inventory records will have an additional member, <code>convertedPrice</code>. This data is then passed to a UI method to update the inventory table.
       
   236 </p>
       
   237 
       
   238 <pre class="code prettyprint">Y.one(&#x27;#demo_go&#x27;).on(&#x27;click&#x27;, function (e) {
       
   239     &#x2F;&#x2F; Disable the button temporarily
       
   240     this.set(&#x27;disabled&#x27;,true);
       
   241 
       
   242     &#x2F;&#x2F; Store the requested currency
       
   243     var sel = Y.one(&quot;#demo select&quot;);
       
   244     example.currency = sel.get(&quot;options&quot;).item(sel.get(&quot;selectedIndex&quot;)).get(&quot;value&quot;);
       
   245 
       
   246     &#x2F;&#x2F; Send the request for the JSON data
       
   247     Y.io(&#x27;..&#x2F;assets&#x2F;json&#x2F;json-convert-values-response.json&#x27;,{
       
   248         timeout : 3000,
       
   249         on : {
       
   250             success : function (xid, res) {
       
   251                 var inventory;
       
   252                 try {
       
   253                     inventory = Y.JSON.parse(res.responseText,example.convert);
       
   254 
       
   255                     example.updateTable(inventory);
       
   256                 }
       
   257                 catch(e) {
       
   258                     alert(&quot;Error getting inventory data&quot;);
       
   259                 }
       
   260                 finally {
       
   261                     Y.one(&#x27;#demo_go&#x27;).set(&#x27;disabled&#x27;,false);
       
   262                 }
       
   263             },
       
   264             failure : function () {
       
   265                 alert(&quot;Error getting inventory data&quot;);
       
   266             }
       
   267         }
       
   268     });
       
   269 });
       
   270 
       
   271 }); &#x2F;&#x2F; End YUI(..).use(..,function (Y) {</pre>
       
   272 
       
   273 
       
   274 <h2>Complete Example Source</h2>
       
   275 
       
   276 <pre class="code prettyprint">&lt;div id=&quot;demo&quot;&gt;
       
   277     &lt;p&gt;Choose a currency, then get the data&lt;&#x2F;p&gt;
       
   278     &lt;select&gt;
       
   279         &lt;option value=&quot;ARS&quot;&gt;Argentine Peso&lt;&#x2F;option&gt;
       
   280         &lt;option value=&quot;AUD&quot;&gt;Australian Dollar&lt;&#x2F;option&gt;
       
   281         &lt;option value=&quot;BRL&quot;&gt;Brazilian Real&lt;&#x2F;option&gt;
       
   282         &lt;option value=&quot;GBP&quot;&gt;British Pound&lt;&#x2F;option&gt;
       
   283         &lt;option value=&quot;CAD&quot;&gt;Canadian Dollar&lt;&#x2F;option&gt;
       
   284         &lt;option value=&quot;CNY&quot;&gt;Chinese Yuan&lt;&#x2F;option&gt;
       
   285         &lt;option value=&quot;COP&quot;&gt;Colombian Peso&lt;&#x2F;option&gt;
       
   286         &lt;option value=&quot;HRK&quot;&gt;Croatian Kuna&lt;&#x2F;option&gt;
       
   287         &lt;option value=&quot;CZK&quot;&gt;Czech Koruna&lt;&#x2F;option&gt;
       
   288         &lt;option value=&quot;DKK&quot;&gt;Danish Krone&lt;&#x2F;option&gt;
       
   289         &lt;option value=&quot;EEK&quot;&gt;Estonian Kroon&lt;&#x2F;option&gt;
       
   290         &lt;option value=&quot;EUR&quot;&gt;Euro&lt;&#x2F;option&gt;
       
   291         &lt;option value=&quot;HKD&quot;&gt;Hong Kong Dollar&lt;&#x2F;option&gt;
       
   292         &lt;option value=&quot;HUF&quot;&gt;Hungarian Forint&lt;&#x2F;option&gt;
       
   293         &lt;option value=&quot;ISK&quot;&gt;Iceland Krona&lt;&#x2F;option&gt;
       
   294         &lt;option value=&quot;INR&quot;&gt;Indian Rupee&lt;&#x2F;option&gt;
       
   295         &lt;option value=&quot;JPY&quot;&gt;Japanese Yen&lt;&#x2F;option&gt;
       
   296         &lt;option value=&quot;KRW&quot;&gt;Korean Won&lt;&#x2F;option&gt;
       
   297         &lt;option value=&quot;LVL&quot;&gt;Latvian Lat&lt;&#x2F;option&gt;
       
   298         &lt;option value=&quot;LTL&quot;&gt;Lithuanian Lita&lt;&#x2F;option&gt;
       
   299         &lt;option value=&quot;MYR&quot;&gt;Malaysian Ringgit&lt;&#x2F;option&gt;
       
   300         &lt;option value=&quot;MXN&quot;&gt;Mexican Peso&lt;&#x2F;option&gt;
       
   301         &lt;option value=&quot;NZD&quot;&gt;New Zealand Dollar&lt;&#x2F;option&gt;
       
   302         &lt;option value=&quot;NOK&quot;&gt;Norwegian Krone&lt;&#x2F;option&gt;
       
   303         &lt;option value=&quot;PHP&quot;&gt;Philippine Peso&lt;&#x2F;option&gt;
       
   304         &lt;option value=&quot;PLN&quot;&gt;Polish Zloty&lt;&#x2F;option&gt;
       
   305         &lt;option value=&quot;RUB&quot;&gt;Russian Rouble&lt;&#x2F;option&gt;
       
   306         &lt;option value=&quot;SGD&quot;&gt;Singapore Dollar&lt;&#x2F;option&gt;
       
   307         &lt;option value=&quot;SKK&quot;&gt;Slovak Koruna&lt;&#x2F;option&gt;
       
   308         &lt;option value=&quot;ZAR&quot;&gt;South African Rand&lt;&#x2F;option&gt;
       
   309         &lt;option value=&quot;LKR&quot;&gt;Sri Lanka Rupee&lt;&#x2F;option&gt;
       
   310         &lt;option value=&quot;SEK&quot;&gt;Swedish Krona&lt;&#x2F;option&gt;
       
   311         &lt;option value=&quot;TRY&quot;&gt;Turkey Lira&lt;&#x2F;option&gt;
       
   312         &lt;option value=&quot;USD&quot; selected=&quot;selected&quot;&gt;U.S. Dollar&lt;&#x2F;option&gt;
       
   313         &lt;option value=&quot;CHF&quot;&gt;Swiss Franc&lt;&#x2F;option&gt;
       
   314         &lt;option value=&quot;TWD&quot;&gt;Taiwan Dollar&lt;&#x2F;option&gt;
       
   315         &lt;option value=&quot;THB&quot;&gt;Thai Baht&lt;&#x2F;option&gt;
       
   316     &lt;&#x2F;select&gt;
       
   317     &lt;input type=&quot;button&quot; id=&quot;demo_go&quot; value=&quot;Get Data&quot;&gt;
       
   318 
       
   319     &lt;table cellspacing=&quot;0&quot;&gt;
       
   320     &lt;caption&gt;Inventory&lt;&#x2F;caption&gt;
       
   321     &lt;thead&gt;
       
   322         &lt;tr&gt;
       
   323             &lt;th&gt;SKU&lt;&#x2F;th&gt;
       
   324             &lt;th&gt;Item&lt;&#x2F;th&gt;
       
   325             &lt;th&gt;Price (USD)&lt;&#x2F;th&gt;
       
   326             &lt;th&gt;Price (&lt;span&gt;USD&lt;&#x2F;span&gt;)&lt;&#x2F;th&gt;
       
   327         &lt;&#x2F;tr&gt;
       
   328     &lt;&#x2F;thead&gt;
       
   329     &lt;tbody&gt;
       
   330         &lt;tr&gt;&lt;td colspan=&quot;4&quot;&gt;Click &lt;em&gt;Get Data&lt;&#x2F;em&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
       
   331     &lt;&#x2F;tbody&gt;
       
   332     &lt;&#x2F;table&gt;
       
   333 &lt;&#x2F;div&gt;
       
   334 
       
   335 &lt;script&gt;
       
   336 YUI().use(&quot;node&quot;, &quot;io&quot;, &quot;json-parse&quot;,function (Y) {
       
   337 
       
   338 &#x2F;&#x2F; Safari 4.0.3&#x27;s native JSON does not support adding members during parse,
       
   339 &#x2F;&#x2F; so use JavaScript implementation for consistency
       
   340 Y.JSON.useNativeParse = false;
       
   341 
       
   342 var example = {
       
   343     rates : {&quot;USD&quot;:1,&quot;EUR&quot;:0.6661,&quot;GBP&quot;:0.5207,&quot;AUD&quot;:1.1225,&quot;BRL&quot;:1.609,&quot;NZD&quot;:1.4198,&quot;CAD&quot;:1.0667,&quot;CHF&quot;:1.0792,&quot;CNY&quot;:6.8587 ,&quot;DKK&quot;:4.9702,&quot;HKD&quot;:7.8064,&quot;INR&quot;:42.0168,&quot;JPY&quot;:109.8901,&quot;KRW&quot;:1000,&quot;LKR&quot;:107.5269,&quot;MXN&quot;:10.1317,&quot;MYR&quot; :3.3167,&quot;NOK&quot;:5.3277,&quot;SEK&quot;:6.2617,&quot;SGD&quot;:1.4073,&quot;THB&quot;:33.7838,&quot;TWD&quot;:31.1526,&quot;VEF&quot;:2.1445,&quot;ZAR&quot;:7.6923 ,&quot;BGN&quot;:1.3028,&quot;CZK&quot;:16.0514,&quot;EEK&quot;:10.4275,&quot;HUF&quot;:158.7302,&quot;LTL&quot;:2.2999,&quot;LVL&quot;:0.4692,&quot;PLN&quot;:2.1758,&quot;RON&quot; :2.3804,&quot;SKK&quot;:20.2429,&quot;ISK&quot;:4.8008,&quot;HRK&quot;:81.3008,&quot;RUB&quot;:24.3309,&quot;TRY&quot;:1.1811,&quot;PHP&quot;:44.2478,&quot;COP&quot;:2000 ,&quot;ARS&quot;:3.1289},
       
   344 
       
   345     currency : &#x27;USD&#x27;,
       
   346 
       
   347     convert : function (k,v) {
       
   348         &#x2F;&#x2F; &#x27;this&#x27; will refer to the object containing the key:value pair,
       
   349         &#x2F;&#x2F; so this step will add a new object member while leaving the original
       
   350         &#x2F;&#x2F; intact (but formatted to two decimal places).  If the original
       
   351         &#x2F;&#x2F; is not needed, just return the converted value.
       
   352         if (k === &#x27;Price&#x27;) {
       
   353             var x = Math.round(v * example.rates[example.currency] * 100) &#x2F; 100;
       
   354             this.convertedPrice = x.toFixed(2); &#x2F;&#x2F; added to item
       
   355             return v.toFixed(2); &#x2F;&#x2F; assigned to item.Price
       
   356         }
       
   357         return v;
       
   358     },
       
   359 
       
   360     updateTable : function (inventory) {
       
   361         &#x2F;&#x2F; Update the column header
       
   362         Y.one(&#x27;#demo table th span&#x27;).set(&#x27;innerHTML&#x27;,example.currency);
       
   363 
       
   364         var tbody = Y.one(&#x27;#demo table tbody&#x27;),
       
   365             html  = [&#x27;&lt;table&gt;&lt;tbody&gt;&#x27;],
       
   366             rowTemplate = &#x27;&lt;tr&gt;&lt;td&gt;{SKU}&lt;&#x2F;td&gt;&lt;td&gt;{Item}&lt;&#x2F;td&gt;&lt;td&gt;{Price}&lt;&#x2F;td&gt;&lt;td&gt;{convertedPrice}&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&#x27;,
       
   367             i, len;
       
   368 
       
   369         if (inventory) {
       
   370             for (i = 0, len = inventory.length; i &lt; len; ++i) {
       
   371                 html.push(Y.Lang.sub(rowTemplate, inventory[i]));
       
   372             }
       
   373         } else {
       
   374             html.push(&#x27;&lt;tr&gt;&lt;td colspan=&quot;4&quot;&gt;No Inventory data&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&#x27;);
       
   375         }
       
   376 
       
   377         html.push(&#x27;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&#x27;);
       
   378 
       
   379         tbody.replace(Y.Node.create(html.join(&#x27;&#x27;)).one(&#x27;tbody&#x27;));
       
   380     }
       
   381 };
       
   382 
       
   383 Y.one(&#x27;#demo_go&#x27;).on(&#x27;click&#x27;, function (e) {
       
   384     &#x2F;&#x2F; Disable the button temporarily
       
   385     this.set(&#x27;disabled&#x27;,true);
       
   386 
       
   387     &#x2F;&#x2F; Store the requested currency
       
   388     var sel = Y.one(&quot;#demo select&quot;);
       
   389     example.currency = sel.get(&quot;options&quot;).item(sel.get(&quot;selectedIndex&quot;)).get(&quot;value&quot;);
       
   390 
       
   391     Y.io(&#x27;..&#x2F;assets&#x2F;json&#x2F;json-convert-values-response.json&#x27;,{
       
   392         timeout : 3000,
       
   393         on : {
       
   394             success : function (xid, res) {
       
   395                 var inventory;
       
   396                 try {
       
   397                     inventory = Y.JSON.parse(res.responseText,example.convert);
       
   398 
       
   399                     example.updateTable(inventory);
       
   400                 }
       
   401                 catch(e) {
       
   402                     alert(&quot;Error getting inventory data&quot;);
       
   403                 }
       
   404                 finally {
       
   405                     Y.one(&#x27;#demo_go&#x27;).set(&#x27;disabled&#x27;,false);
       
   406                 }
       
   407             },
       
   408             failure : function () {
       
   409                 alert(&quot;Error getting inventory data&quot;);
       
   410             }
       
   411         }
       
   412     });
       
   413 });
       
   414 
       
   415 &#x2F;&#x2F; Expose example objects for inspection
       
   416 YUI.example = example;
       
   417 });
       
   418 &lt;&#x2F;script&gt;</pre>
       
   419 
       
   420 </div>
       
   421             </div>
       
   422         </div>
       
   423 
       
   424         <div class="yui3-u-1-4">
       
   425             <div class="sidebar">
       
   426                 
       
   427 
       
   428                 
       
   429                     <div class="sidebox">
       
   430                         <div class="hd">
       
   431                             <h2 class="no-toc">Examples</h2>
       
   432                         </div>
       
   433 
       
   434                         <div class="bd">
       
   435                             <ul class="examples">
       
   436                                 
       
   437                                     
       
   438                                         <li data-description="Use JSON to parse data received via XMLHttpRequest via Y.io calls — a simple JSON use case.">
       
   439                                             <a href="json-connect.html">JSON with Y.io</a>
       
   440                                         </li>
       
   441                                     
       
   442                                 
       
   443                                     
       
   444                                         <li data-description="Use the replacer and reviver parameters to reconstitute object instances that have been serialized to JSON.">
       
   445                                             <a href="json-freeze-thaw.html">Rebuilding Class Instances from JSON Data</a>
       
   446                                         </li>
       
   447                                     
       
   448                                 
       
   449                                     
       
   450                                         <li data-description="Use a currency conversion calculation to add a new price member to a JSON response, demonstrating how JSON data, once retrieved, can be transformed during parsing.">
       
   451                                             <a href="json-convert-values.html">Adding New Object Members During Parsing</a>
       
   452                                         </li>
       
   453                                     
       
   454                                 
       
   455                                     
       
   456                                 
       
   457                                     
       
   458                                 
       
   459                             </ul>
       
   460                         </div>
       
   461                     </div>
       
   462                 
       
   463 
       
   464                 
       
   465                     <div class="sidebox">
       
   466                         <div class="hd">
       
   467                             <h2 class="no-toc">Examples That Use This Component</h2>
       
   468                         </div>
       
   469 
       
   470                         <div class="bd">
       
   471                             <ul class="examples">
       
   472                                 
       
   473                                     
       
   474                                 
       
   475                                     
       
   476                                 
       
   477                                     
       
   478                                 
       
   479                                     
       
   480                                         <li data-description="A basic todo list built with the Model, Model List, and View components.">
       
   481                                             <a href="../app/app-todo.html">Todo List</a>
       
   482                                         </li>
       
   483                                     
       
   484                                 
       
   485                                     
       
   486                                         <li data-description="Portal style example using Drag &amp; Drop Event Bubbling and Animation.">
       
   487                                             <a href="../dd/portal-drag.html">Portal Style Example</a>
       
   488                                         </li>
       
   489                                     
       
   490                                 
       
   491                             </ul>
       
   492                         </div>
       
   493                     </div>
       
   494                 
       
   495             </div>
       
   496         </div>
       
   497     </div>
       
   498 </div>
       
   499 
       
   500 <script src="../assets/vendor/prettify/prettify-min.js"></script>
       
   501 <script>prettyPrint();</script>
       
   502 
       
   503 <script>
       
   504 YUI.Env.Tests = {
       
   505     examples: [],
       
   506     project: '../assets',
       
   507     assets: '../assets/json',
       
   508     name: 'json-convert-values',
       
   509     title: 'Adding New Object Members During Parsing',
       
   510     newWindow: '',
       
   511     auto:  false 
       
   512 };
       
   513 YUI.Env.Tests.examples.push('json-connect');
       
   514 YUI.Env.Tests.examples.push('json-freeze-thaw');
       
   515 YUI.Env.Tests.examples.push('json-convert-values');
       
   516 YUI.Env.Tests.examples.push('app-todo');
       
   517 YUI.Env.Tests.examples.push('portal-drag');
       
   518 
       
   519 </script>
       
   520 <script src="../assets/yui/test-runner.js"></script>
       
   521 
       
   522 
       
   523 
       
   524 </body>
       
   525 </html>