Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / direct / JsonProvider.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.direct.JsonProvider
17  * @extends Ext.direct.Provider
18
19 A base provider for communicating using JSON. This is an abstract class
20 and should not be instanced directly.
21
22  * @markdown
23  * @abstract
24  */
25
26 Ext.define('Ext.direct.JsonProvider', {
27
28     /* Begin Definitions */
29
30     extend: 'Ext.direct.Provider',
31
32     alias: 'direct.jsonprovider',
33
34     uses: ['Ext.direct.ExceptionEvent'],
35
36     /* End Definitions */
37
38    /**
39     * Parse the JSON response
40     * @private
41     * @param {Object} response The XHR response object
42     * @return {Object} The data in the response.
43     */
44    parseResponse: function(response){
45         if (!Ext.isEmpty(response.responseText)) {
46             if (Ext.isObject(response.responseText)) {
47                 return response.responseText;
48             }
49             return Ext.decode(response.responseText);
50         }
51         return null;
52     },
53
54     /**
55      * Creates a set of events based on the XHR response
56      * @private
57      * @param {Object} response The XHR response
58      * @return {Ext.direct.Event[]} An array of Ext.direct.Event
59      */
60     createEvents: function(response){
61         var data = null,
62             events = [],
63             event,
64             i = 0,
65             len;
66
67         try{
68             data = this.parseResponse(response);
69         } catch(e) {
70             event = Ext.create('Ext.direct.ExceptionEvent', {
71                 data: e,
72                 xhr: response,
73                 code: Ext.direct.Manager.self.exceptions.PARSE,
74                 message: 'Error parsing json response: \n\n ' + data
75             });
76             return [event];
77         }
78
79         if (Ext.isArray(data)) {
80             for (len = data.length; i < len; ++i) {
81                 events.push(this.createEvent(data[i]));
82             }
83         } else {
84             events.push(this.createEvent(data));
85         }
86         return events;
87     },
88
89     /**
90      * Create an event from a response object
91      * @param {Object} response The XHR response object
92      * @return {Ext.direct.Event} The event
93      */
94     createEvent: function(response){
95         return Ext.create('direct.' + response.type, response);
96     }
97 });