~ubuntu-branches/ubuntu/precise/whoopsie-daisy/precise-proposed

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/docs/json/json-connect.html

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-04-18 13:04:36 UTC
  • Revision ID: package-import@ubuntu.com-20120418130436-vmt93p8fds516lws
Tags: 0.1.32
Fix failing tests on powerpc and ARM.

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: JSON with Y.io</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: JSON with Y.io</h1>
 
16
 
 
17
    
 
18
 
 
19
    <div class="yui3-g">
 
20
        <div class="yui3-u-3-4">
 
21
            <div id="main">
 
22
                <div class="content"><div class="intro">
 
23
<p>
 
24
    A natural fit for the JSON module is to work with IO to parse JSON messages received via XMLHttpRequest (XHR). In this example, we'll request JSON data from the server using the <a href="http://yuilibrary.com/yui/docs/api/classes/IO.html#method_io"><code>Y.io()</code></a> method and parse the resulting JSON string data.
 
25
</p>
 
26
</div>
 
27
 
 
28
<div class="example yui3-skin-sam">
 
29
    <div id="demo" class="yui3-skin-sam">
 
30
    <p>Click the <em>Get Messages</em> button to send the request to the server; the response will be displayed below the button.</p>
 
31
    <p><button>Get Messages</button></p>
 
32
    <div id="demo-messages"></div>
 
33
</div>
 
34
 
 
35
<script>
 
36
// Create business logic in a YUI sandbox using the 'io' and 'json' modules
 
37
YUI().use("node", "io", "dump", "json-parse", function (Y) {
 
38
 
 
39
    // capture the node that we'll display the messages in
 
40
    var target = Y.one('#demo-messages');
 
41
 
 
42
    // Create the io callback/configuration
 
43
    var callback = {
 
44
 
 
45
        timeout : 3000,
 
46
 
 
47
        on : {
 
48
            success : function (x,o) {
 
49
                Y.log("RAW JSON DATA: " + o.responseText);
 
50
 
 
51
                var messages = [],
 
52
                    html = '', i, l;
 
53
 
 
54
                // Process the JSON data returned from the server
 
55
                try {
 
56
                    messages = Y.JSON.parse(o.responseText);
 
57
                }
 
58
                catch (e) {
 
59
                    alert("JSON Parse failed!");
 
60
                    return;
 
61
                }
 
62
 
 
63
                Y.log("PARSED DATA: " + Y.Lang.dump(messages));
 
64
 
 
65
                // The returned data was parsed into an array of objects.
 
66
                // Add a P element for each received message
 
67
                for (i=0, l=messages.length; i < l; ++i) {
 
68
                    html += '<p>' + messages[i].animal + ' says "' +
 
69
                                    messages[i].message + '"</p>';
 
70
                }
 
71
 
 
72
                // Use the Node API to apply the new innerHTML to the target
 
73
                target.setContent(html);
 
74
            },
 
75
 
 
76
            failure : function (x,o) {
 
77
                alert("Async call failed!");
 
78
            }
 
79
 
 
80
        }
 
81
    };
 
82
 
 
83
    // Attach a click event listener to the button #demo_btn to send the request
 
84
    Y.one('#demo button').on('click',function (e) {
 
85
        // Make the call to the server for JSON data
 
86
        Y.io("../assets/json/json-connect-response.json", callback);
 
87
    });
 
88
 
 
89
});
 
90
</script>
 
91
 
 
92
</div>
 
93
 
 
94
<h2>Use <code>Y.JSON.parse</code> in the <code>success</code> handler</h2>
 
95
 
 
96
<p>
 
97
    Pass the XHR <code>responseText</code> to <code>Y.JSON.parse()</code> and capture the return value. Note that the <code>parse()</code> method can throw a <code>SyntaxError</code> exception, so be sure to wrap the call in a <code>try&#x2F;catch</code> block.
 
98
</p>
 
99
 
 
100
<pre class="code prettyprint lang-javascript">Y.io(&#x27;..&#x2F;assets&#x2F;json&#x2F;json-connect-response.json&#x27;, {
 
101
    on : {
 
102
        success : function (tx, r) {
 
103
            var parsedResponse;
 
104
 
 
105
            &#x2F;&#x2F; protected against malformed JSON response
 
106
            try {
 
107
               parsedResponse = Y.JSON.parse(r.responseText);
 
108
            }
 
109
            catch (e) {
 
110
                alert(&quot;JSON Parse failed!&quot;);
 
111
                return;
 
112
            }
 
113
        }
 
114
    }
 
115
});</pre>
 
116
 
 
117
 
 
118
<p>
 
119
    The <code>parse()</code> method returns the native JavaScript object representation of the string data returned from the <a href="http://yuilibrary.com/yui/docs/api/classes/IO.html#method_io"><code>Y.io()</code></a> call. In this case, the data is an array of object literals in this form:
 
120
</p>
 
121
 
 
122
<pre class="code prettyprint lang-json">[
 
123
    { &quot;animal&quot; : &quot;Cat&quot;,  &quot;message&quot; : &quot;Meow&quot;  },
 
124
    { &quot;animal&quot; : &quot;Dog&quot;,  &quot;message&quot; : &quot;Woof&quot;  },
 
125
    { &quot;animal&quot; : &quot;Cow&quot;,  &quot;message&quot; : &quot;Moo&quot;   },
 
126
    { &quot;animal&quot; : &quot;Duck&quot;, &quot;message&quot; : &quot;Quack&quot; },
 
127
    { &quot;animal&quot; : &quot;Lion&quot;, &quot;message&quot; : &quot;Roar&quot;  }
 
128
]</pre>
 
129
 
 
130
 
 
131
<p>
 
132
    In the <code>success</code> handler we'll simply loop through this array and outputting its contents to the DOM.
 
133
</p>
 
134
 
 
135
<h2>Complete Example Source</h2>
 
136
<pre class="code prettyprint">&lt;div id=&quot;demo&quot; class=&quot;yui3-skin-sam&quot;&gt;
 
137
    &lt;p&gt;Click the &lt;em&gt;Get Messages&lt;&#x2F;em&gt; button to send the request to the server; the response will be displayed below the button.&lt;&#x2F;p&gt;
 
138
    &lt;p&gt;&lt;button&gt;Get Messages&lt;&#x2F;button&gt;&lt;&#x2F;p&gt;
 
139
    &lt;div id=&quot;demo-messages&quot;&gt;&lt;&#x2F;div&gt;
 
140
&lt;&#x2F;div&gt;
 
141
 
 
142
&lt;script&gt;
 
143
&#x2F;&#x2F; Create business logic in a YUI sandbox using the &#x27;io&#x27; and &#x27;json&#x27; modules
 
144
YUI().use(&quot;node&quot;, &quot;io&quot;, &quot;dump&quot;, &quot;json-parse&quot;, function (Y) {
 
145
 
 
146
    &#x2F;&#x2F; capture the node that we&#x27;ll display the messages in
 
147
    var target = Y.one(&#x27;#demo-messages&#x27;);
 
148
 
 
149
    &#x2F;&#x2F; Create the io callback&#x2F;configuration
 
150
    var callback = {
 
151
 
 
152
        timeout : 3000,
 
153
 
 
154
        on : {
 
155
            success : function (x,o) {
 
156
                Y.log(&quot;RAW JSON DATA: &quot; + o.responseText);
 
157
 
 
158
                var messages = [],
 
159
                    html = &#x27;&#x27;, i, l;
 
160
 
 
161
                &#x2F;&#x2F; Process the JSON data returned from the server
 
162
                try {
 
163
                    messages = Y.JSON.parse(o.responseText);
 
164
                }
 
165
                catch (e) {
 
166
                    alert(&quot;JSON Parse failed!&quot;);
 
167
                    return;
 
168
                }
 
169
 
 
170
                Y.log(&quot;PARSED DATA: &quot; + Y.Lang.dump(messages));
 
171
 
 
172
                &#x2F;&#x2F; The returned data was parsed into an array of objects.
 
173
                &#x2F;&#x2F; Add a P element for each received message
 
174
                for (i=0, l=messages.length; i &lt; l; ++i) {
 
175
                    html += &#x27;&lt;p&gt;&#x27; + messages[i].animal + &#x27; says &quot;&#x27; +
 
176
                                    messages[i].message + &#x27;&quot;&lt;&#x2F;p&gt;&#x27;;
 
177
                }
 
178
 
 
179
                &#x2F;&#x2F; Use the Node API to apply the new innerHTML to the target
 
180
                target.setContent(html);
 
181
            },
 
182
 
 
183
            failure : function (x,o) {
 
184
                alert(&quot;Async call failed!&quot;);
 
185
            }
 
186
 
 
187
        }
 
188
    };
 
189
 
 
190
    &#x2F;&#x2F; Attach a click event listener to the button #demo_btn to send the request
 
191
    Y.one(&#x27;#demo button&#x27;).on(&#x27;click&#x27;,function (e) {
 
192
        &#x2F;&#x2F; Make the call to the server for JSON data
 
193
        Y.io(&quot;..&#x2F;assets&#x2F;json&#x2F;json-connect-response.json&quot;, callback);
 
194
    });
 
195
 
 
196
});
 
197
&lt;&#x2F;script&gt;</pre>
 
198
 
 
199
</div>
 
200
            </div>
 
201
        </div>
 
202
 
 
203
        <div class="yui3-u-1-4">
 
204
            <div class="sidebar">
 
205
                
 
206
 
 
207
                
 
208
                    <div class="sidebox">
 
209
                        <div class="hd">
 
210
                            <h2 class="no-toc">Examples</h2>
 
211
                        </div>
 
212
 
 
213
                        <div class="bd">
 
214
                            <ul class="examples">
 
215
                                
 
216
                                    
 
217
                                        <li data-description="Use JSON to parse data received via XMLHttpRequest via Y.io calls — a simple JSON use case.">
 
218
                                            <a href="json-connect.html">JSON with Y.io</a>
 
219
                                        </li>
 
220
                                    
 
221
                                
 
222
                                    
 
223
                                        <li data-description="Use the replacer and reviver parameters to reconstitute object instances that have been serialized to JSON.">
 
224
                                            <a href="json-freeze-thaw.html">Rebuilding Class Instances from JSON Data</a>
 
225
                                        </li>
 
226
                                    
 
227
                                
 
228
                                    
 
229
                                        <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.">
 
230
                                            <a href="json-convert-values.html">Adding New Object Members During Parsing</a>
 
231
                                        </li>
 
232
                                    
 
233
                                
 
234
                                    
 
235
                                
 
236
                                    
 
237
                                
 
238
                            </ul>
 
239
                        </div>
 
240
                    </div>
 
241
                
 
242
 
 
243
                
 
244
                    <div class="sidebox">
 
245
                        <div class="hd">
 
246
                            <h2 class="no-toc">Examples That Use This Component</h2>
 
247
                        </div>
 
248
 
 
249
                        <div class="bd">
 
250
                            <ul class="examples">
 
251
                                
 
252
                                    
 
253
                                
 
254
                                    
 
255
                                
 
256
                                    
 
257
                                
 
258
                                    
 
259
                                        <li data-description="A basic todo list built with the Model, Model List, and View components.">
 
260
                                            <a href="../app/app-todo.html">Todo List</a>
 
261
                                        </li>
 
262
                                    
 
263
                                
 
264
                                    
 
265
                                        <li data-description="Portal style example using Drag &amp; Drop Event Bubbling and Animation.">
 
266
                                            <a href="../dd/portal-drag.html">Portal Style Example</a>
 
267
                                        </li>
 
268
                                    
 
269
                                
 
270
                            </ul>
 
271
                        </div>
 
272
                    </div>
 
273
                
 
274
            </div>
 
275
        </div>
 
276
    </div>
 
277
</div>
 
278
 
 
279
<script src="../assets/vendor/prettify/prettify-min.js"></script>
 
280
<script>prettyPrint();</script>
 
281
 
 
282
</body>
 
283
</html>