commit extjs-2.2.1
[extjs.git] / source / data / HttpProxy.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.data.HttpProxy\r
11  * @extends Ext.data.DataProxy\r
12  * An implementation of {@link Ext.data.DataProxy} that reads a data object from a {@link Ext.data.Connection Connection} object\r
13  * configured to reference a certain URL.<br>\r
14  * <p>\r
15  * <b>Note that this class cannot be used to retrieve data from a domain other than the domain\r
16  * from which the running page was served.<br>\r
17  * <p>\r
18  * For cross-domain access to remote data, use a {@link Ext.data.ScriptTagProxy ScriptTagProxy}.</b><br>\r
19  * <p>\r
20  * Be aware that to enable the browser to parse an XML document, the server must set\r
21  * the Content-Type header in the HTTP response to "text/xml".\r
22  * @constructor\r
23  * @param {Object} conn an {@link Ext.data.Connection} object, or options parameter to {@link Ext.Ajax#request}.\r
24  * If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make the request.\r
25  */\r
26 Ext.data.HttpProxy = function(conn){\r
27     Ext.data.HttpProxy.superclass.constructor.call(this);\r
28     /**\r
29      * The Connection object (Or options parameter to {@link Ext.Ajax#request}) which this HttpProxy uses to make requests to the server.\r
30      * Properties of this object may be changed dynamically to change the way data is requested.\r
31      * @property\r
32      */\r
33     this.conn = conn;\r
34     this.useAjax = !conn || !conn.events;\r
35 \r
36     /**\r
37      * @event loadexception\r
38      * Fires if an exception occurs in the Proxy during data loading.  This event can be fired for one of two reasons:\r
39      * <ul><li><b>The load call returned success: false.</b>  This means the server logic returned a failure\r
40      * status and there is no data to read.  In this case, this event will be raised and the\r
41      * fourth parameter (read error) will be null.</li>\r
42      * <li><b>The load succeeded but the reader could not read the response.</b>  This means the server returned\r
43      * data, but the configured Reader threw an error while reading the data.  In this case, this event will be \r
44      * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>\r
45      * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly\r
46      * on any Store instance.\r
47      * @param {Object} this\r
48      * @param {Object} options The loading options that were specified (see {@link #load} for details)\r
49      * @param {Object} response The XMLHttpRequest object containing the response data\r
50      * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.\r
51      * If the load call returned success: false, this parameter will be null.\r
52      */\r
53 };\r
54 \r
55 Ext.extend(Ext.data.HttpProxy, Ext.data.DataProxy, {\r
56     /**\r
57      * Return the {@link Ext.data.Connection} object being used by this Proxy.\r
58      * @return {Connection} The Connection object. This object may be used to subscribe to events on\r
59      * a finer-grained basis than the DataProxy events.\r
60      */\r
61     getConnection : function(){\r
62         return this.useAjax ? Ext.Ajax : this.conn;\r
63     },\r
64 \r
65     /**\r
66      * Load data from the configured {@link Ext.data.Connection}, read the data object into\r
67      * a block of Ext.data.Records using the passed {@link Ext.data.DataReader} implementation, and\r
68      * process that block using the passed callback.\r
69      * @param {Object} params An object containing properties which are to be used as HTTP parameters\r
70      * for the request to the remote server.\r
71      * @param {Ext.data.DataReader} reader The Reader object which converts the data\r
72      * object into a block of Ext.data.Records.\r
73      * @param {Function} callback The function into which to pass the block of Ext.data.Records.\r
74      * The function must be passed <ul>\r
75      * <li>The Record block object</li>\r
76      * <li>The "arg" argument from the load function</li>\r
77      * <li>A boolean success indicator</li>\r
78      * </ul>\r
79      * @param {Object} scope The scope in which to call the callback\r
80      * @param {Object} arg An optional argument which is passed to the callback as its second parameter.\r
81      */\r
82     load : function(params, reader, callback, scope, arg){\r
83         if(this.fireEvent("beforeload", this, params) !== false){\r
84             var  o = {\r
85                 params : params || {},\r
86                 request: {\r
87                     callback : callback,\r
88                     scope : scope,\r
89                     arg : arg\r
90                 },\r
91                 reader: reader,\r
92                 callback : this.loadResponse,\r
93                 scope: this\r
94             };\r
95             if(this.useAjax){\r
96                 Ext.applyIf(o, this.conn);\r
97                 if(this.activeRequest){\r
98                     Ext.Ajax.abort(this.activeRequest);\r
99                 }\r
100                 this.activeRequest = Ext.Ajax.request(o);\r
101             }else{\r
102                 this.conn.request(o);\r
103             }\r
104         }else{\r
105             callback.call(scope||this, null, arg, false);\r
106         }\r
107     },\r
108 \r
109     // private\r
110     loadResponse : function(o, success, response){\r
111         delete this.activeRequest;\r
112         if(!success){\r
113             this.fireEvent("loadexception", this, o, response);\r
114             o.request.callback.call(o.request.scope, null, o.request.arg, false);\r
115             return;\r
116         }\r
117         var result;\r
118         try {\r
119             result = o.reader.read(response);\r
120         }catch(e){\r
121             this.fireEvent("loadexception", this, o, response, e);\r
122             o.request.callback.call(o.request.scope, null, o.request.arg, false);\r
123             return;\r
124         }\r
125         this.fireEvent("load", this, o, o.request.arg);\r
126         o.request.callback.call(o.request.scope, result, o.request.arg, true);\r
127     },\r
128     \r
129     // private\r
130     update : function(dataSet){\r
131         \r
132     },\r
133     \r
134     // private\r
135     updateResponse : function(dataSet){\r
136         \r
137     }\r
138 });