Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / docs / source / JsonProvider.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-direct-JsonProvider'>/**
19 </span> * @class Ext.direct.JsonProvider
20  * @extends Ext.direct.Provider
21
22 A base provider for communicating using JSON. This is an abstract class
23 and should not be instanced directly.
24
25  * @markdown
26  * @abstract
27  */
28
29 Ext.define('Ext.direct.JsonProvider', {
30
31     /* Begin Definitions */
32
33     extend: 'Ext.direct.Provider',
34
35     alias: 'direct.jsonprovider',
36
37     uses: ['Ext.direct.ExceptionEvent'],
38
39     /* End Definitions */
40
41 <span id='Ext-direct-JsonProvider-method-parseResponse'>   /**
42 </span>    * Parse the JSON response
43     * @private
44     * @param {Object} response The XHR response object
45     * @return {Object} The data in the response.
46     */
47    parseResponse: function(response){
48         if (!Ext.isEmpty(response.responseText)) {
49             if (Ext.isObject(response.responseText)) {
50                 return response.responseText;
51             }
52             return Ext.decode(response.responseText);
53         }
54         return null;
55     },
56
57 <span id='Ext-direct-JsonProvider-method-createEvents'>    /**
58 </span>     * Creates a set of events based on the XHR response
59      * @private
60      * @param {Object} response The XHR response
61      * @return {Ext.direct.Event[]} An array of Ext.direct.Event
62      */
63     createEvents: function(response){
64         var data = null,
65             events = [],
66             event,
67             i = 0,
68             len;
69
70         try{
71             data = this.parseResponse(response);
72         } catch(e) {
73             event = Ext.create('Ext.direct.ExceptionEvent', {
74                 data: e,
75                 xhr: response,
76                 code: Ext.direct.Manager.self.exceptions.PARSE,
77                 message: 'Error parsing json response: \n\n ' + data
78             });
79             return [event];
80         }
81
82         if (Ext.isArray(data)) {
83             for (len = data.length; i &lt; len; ++i) {
84                 events.push(this.createEvent(data[i]));
85             }
86         } else {
87             events.push(this.createEvent(data));
88         }
89         return events;
90     },
91
92 <span id='Ext-direct-JsonProvider-method-createEvent'>    /**
93 </span>     * Create an event from a response object
94      * @param {Object} response The XHR response object
95      * @return {Ext.direct.Event} The event
96      */
97     createEvent: function(response){
98         return Ext.create('direct.' + response.type, response);
99     }
100 });</pre>
101 </body>
102 </html>