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