Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / HttpProxy.html
1 <html>
2 <head>
3   <title>The source code</title>
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
14 <div id="cls-Ext.data.HttpProxy"></div>/**\r
15  * @class Ext.data.HttpProxy\r
16  * @extends Ext.data.DataProxy\r
17  * <p>An implementation of {@link Ext.data.DataProxy} that processes data requests within the same\r
18  * domain of the originating page.</p>\r
19  * <p><b>Note</b>: this class cannot be used to retrieve data from a domain other\r
20  * than the domain from which the running page was served. For cross-domain requests, use a\r
21  * {@link Ext.data.ScriptTagProxy ScriptTagProxy}.</p>\r
22  * <p>Be aware that to enable the browser to parse an XML document, the server must set\r
23  * the Content-Type header in the HTTP response to "<tt>text/xml</tt>".</p>\r
24  * @constructor\r
25  * @param {Object} conn\r
26  * An {@link Ext.data.Connection} object, or options parameter to {@link Ext.Ajax#request}.\r
27  * <p>Note that if this HttpProxy is being used by a (@link Ext.data.Store Store}, then the\r
28  * Store's call to {@link #load} will override any specified <tt>callback</tt> and <tt>params</tt>\r
29  * options. In this case, use the Store's {@link Ext.data.Store#events events} to modify parameters,\r
30  * or react to loading events. The Store's {@link Ext.data.Store#baseParams baseParams} may also be\r
31  * used to pass parameters known at instantiation time.</p>\r
32  * <p>If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make\r
33  * the request.</p>\r
34  */\r
35 Ext.data.HttpProxy = function(conn){\r
36     Ext.data.HttpProxy.superclass.constructor.call(this, conn);\r
37 \r
38     <div id="prop-Ext.data.HttpProxy-conn"></div>/**\r
39      * The Connection object (Or options parameter to {@link Ext.Ajax#request}) which this HttpProxy\r
40      * uses to make requests to the server. Properties of this object may be changed dynamically to\r
41      * change the way data is requested.\r
42      * @property\r
43      */\r
44     this.conn = conn;\r
45 \r
46     // nullify the connection url.  The url param has been copied to 'this' above.  The connection\r
47     // url will be set during each execution of doRequest when buildUrl is called.  This makes it easier for users to override the\r
48     // connection url during beforeaction events (ie: beforeload, beforewrite, etc).\r
49     // Url is always re-defined during doRequest.\r
50     this.conn.url = null;\r
51 \r
52     this.useAjax = !conn || !conn.events;\r
53 \r
54     // A hash containing active requests, keyed on action [Ext.data.Api.actions.create|read|update|destroy]\r
55     var actions = Ext.data.Api.actions;\r
56     this.activeRequest = {};\r
57     for (var verb in actions) {\r
58         this.activeRequest[actions[verb]] = undefined;\r
59     }\r
60 };\r
61 \r
62 Ext.extend(Ext.data.HttpProxy, Ext.data.DataProxy, {\r
63     <div id="method-Ext.data.HttpProxy-getConnection"></div>/**\r
64      * Return the {@link Ext.data.Connection} object being used by this Proxy.\r
65      * @return {Connection} The Connection object. This object may be used to subscribe to events on\r
66      * a finer-grained basis than the DataProxy events.\r
67      */\r
68     getConnection : function() {\r
69         return this.useAjax ? Ext.Ajax : this.conn;\r
70     },\r
71 \r
72     <div id="method-Ext.data.HttpProxy-setUrl"></div>/**\r
73      * Used for overriding the url used for a single request.  Designed to be called during a beforeaction event.  Calling setUrl\r
74      * will override any urls set via the api configuration parameter.  Set the optional parameter makePermanent to set the url for\r
75      * all subsequent requests.  If not set to makePermanent, the next request will use the same url or api configuration defined\r
76      * in the initial proxy configuration.\r
77      * @param {String} url\r
78      * @param {Boolean} makePermanent (Optional) [false]\r
79      *\r
80      * (e.g.: beforeload, beforesave, etc).\r
81      */\r
82     setUrl : function(url, makePermanent) {\r
83         this.conn.url = url;\r
84         if (makePermanent === true) {\r
85             this.url = url;\r
86             this.api = null;\r
87             Ext.data.Api.prepare(this);\r
88         }\r
89     },\r
90 \r
91     <div id="method-Ext.data.HttpProxy-doRequest"></div>/**\r
92      * HttpProxy implementation of DataProxy#doRequest\r
93      * @param {String} action The crud action type (create, read, update, destroy)\r
94      * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null\r
95      * @param {Object} params An object containing properties which are to be used as HTTP parameters\r
96      * for the request to the remote server.\r
97      * @param {Ext.data.DataReader} reader The Reader object which converts the data\r
98      * object into a block of Ext.data.Records.\r
99      * @param {Function} callback\r
100      * <div class="sub-desc"><p>A function to be called after the request.\r
101      * The <tt>callback</tt> is passed the following arguments:<ul>\r
102      * <li><tt>r</tt> : Ext.data.Record[] The block of Ext.data.Records.</li>\r
103      * <li><tt>options</tt>: Options object from the action request</li>\r
104      * <li><tt>success</tt>: Boolean success indicator</li></ul></p></div>\r
105      * @param {Object} scope The scope in which to call the callback\r
106      * @param {Object} arg An optional argument which is passed to the callback as its second parameter.\r
107      */\r
108     doRequest : function(action, rs, params, reader, cb, scope, arg) {\r
109         var  o = {\r
110             method: (this.api[action]) ? this.api[action]['method'] : undefined,\r
111             request: {\r
112                 callback : cb,\r
113                 scope : scope,\r
114                 arg : arg\r
115             },\r
116             reader: reader,\r
117             callback : this.createCallback(action, rs),\r
118             scope: this\r
119         };\r
120 \r
121         // If possible, transmit data using jsonData || xmlData on Ext.Ajax.request (An installed DataWriter would have written it there.).\r
122         // Use std HTTP params otherwise.\r
123         // TODO wrap into 1 Ext.apply now?\r
124         if (params.jsonData) {\r
125             o.jsonData = params.jsonData;\r
126         } else if (params.xmlData) {\r
127             o.xmlData = params.xmlData;\r
128         } else {\r
129             o.params = params || {};\r
130         }\r
131         // Set the connection url.  If this.conn.url is not null here,\r
132         // the user may have overridden the url during a beforeaction event-handler.\r
133         // this.conn.url is nullified after each request.\r
134         if (this.conn.url === null) {\r
135             this.conn.url = this.buildUrl(action, rs);\r
136         }\r
137         else if (this.restful === true && rs instanceof Ext.data.Record && !rs.phantom) { // <-- user must have intervened with #setApi or #setUrl\r
138             this.conn.url += '/' + rs.id;\r
139         }\r
140         if(this.useAjax){\r
141 \r
142             Ext.applyIf(o, this.conn);\r
143 \r
144             // If a currently running request is found for this action, abort it.\r
145             if (this.activeRequest[action]) {\r
146                 ////\r
147                 // Disabled aborting activeRequest while implementing REST.  activeRequest[action] will have to become an array\r
148                 // TODO ideas anyone?\r
149                 //\r
150                 //Ext.Ajax.abort(this.activeRequest[action]);\r
151             }\r
152             this.activeRequest[action] = Ext.Ajax.request(o);\r
153         }else{\r
154             this.conn.request(o);\r
155         }\r
156         // request is sent, nullify the connection url in preparation for the next request\r
157         this.conn.url = null;\r
158     },\r
159 \r
160     /**\r
161      * Returns a callback function for a request.  Note a special case is made for the\r
162      * read action vs all the others.\r
163      * @param {String} action [create|update|delete|load]\r
164      * @param {Ext.data.Record[]} rs The Store-recordset being acted upon\r
165      * @private\r
166      */\r
167     createCallback : function(action, rs) {\r
168         return function(o, success, response) {\r
169             this.activeRequest[action] = undefined;\r
170             if (!success) {\r
171                 if (action === Ext.data.Api.actions.read) {\r
172                     // @deprecated: fire loadexception for backwards compat.\r
173                     // TODO remove in 3.1\r
174                     this.fireEvent('loadexception', this, o, response);\r
175                 }\r
176                 this.fireEvent('exception', this, 'response', action, o, response);\r
177                 o.request.callback.call(o.request.scope, null, o.request.arg, false);\r
178                 return;\r
179             }\r
180             if (action === Ext.data.Api.actions.read) {\r
181                 this.onRead(action, o, response);\r
182             } else {\r
183                 this.onWrite(action, o, response, rs);\r
184             }\r
185         }\r
186     },\r
187 \r
188     /**\r
189      * Callback for read action\r
190      * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.\r
191      * @param {Object} o The request transaction object\r
192      * @param {Object} res The server response\r
193      * @fires loadexception (deprecated)\r
194      * @fires exception\r
195      * @fires load\r
196      * @private\r
197      */\r
198     onRead : function(action, o, response) {\r
199         var result;\r
200         try {\r
201             result = o.reader.read(response);\r
202         }catch(e){\r
203             // @deprecated: fire old loadexception for backwards-compat.\r
204             // TODO remove in 3.1\r
205             this.fireEvent('loadexception', this, o, response, e);\r
206 \r
207             this.fireEvent('exception', this, 'response', action, o, response, e);\r
208             o.request.callback.call(o.request.scope, null, o.request.arg, false);\r
209             return;\r
210         }\r
211         if (result.success === false) {\r
212             // @deprecated: fire old loadexception for backwards-compat.\r
213             // TODO remove in 3.1\r
214             this.fireEvent('loadexception', this, o, response);\r
215 \r
216             // Get DataReader read-back a response-object to pass along to exception event\r
217             var res = o.reader.readResponse(action, response);\r
218             this.fireEvent('exception', this, 'remote', action, o, res, null);\r
219         }\r
220         else {\r
221             this.fireEvent('load', this, o, o.request.arg);\r
222         }\r
223         // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance\r
224         // the calls to request.callback(...) in each will have to be made identical.\r
225         // NOTE reader.readResponse does not currently return Ext.data.Response\r
226         o.request.callback.call(o.request.scope, result, o.request.arg, result.success);\r
227     },\r
228     /**\r
229      * Callback for write actions\r
230      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
231      * @param {Object} trans The request transaction object\r
232      * @param {Object} res The server response\r
233      * @fires exception\r
234      * @fires write\r
235      * @private\r
236      */\r
237     onWrite : function(action, o, response, rs) {\r
238         var reader = o.reader;\r
239         var res;\r
240         try {\r
241             res = reader.readResponse(action, response);\r
242         } catch (e) {\r
243             this.fireEvent('exception', this, 'response', action, o, response, e);\r
244             o.request.callback.call(o.request.scope, null, o.request.arg, false);\r
245             return;\r
246         }\r
247         if (res.success === false) {\r
248             this.fireEvent('exception', this, 'remote', action, o, res, rs);\r
249         } else {\r
250             this.fireEvent('write', this, action, res.data, res, rs, o.request.arg);\r
251         }\r
252         // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance\r
253         // the calls to request.callback(...) in each will have to be made similar.\r
254         // NOTE reader.readResponse does not currently return Ext.data.Response\r
255         o.request.callback.call(o.request.scope, res.data, res, res.success);\r
256     },\r
257 \r
258     // inherit docs\r
259     destroy: function(){\r
260         if(!this.useAjax){\r
261             this.conn.abort();\r
262         }else if(this.activeRequest){\r
263             var actions = Ext.data.Api.actions;\r
264             for (var verb in actions) {\r
265                 if(this.activeRequest[actions[verb]]){\r
266                     Ext.Ajax.abort(this.activeRequest[actions[verb]]);\r
267                 }\r
268             }\r
269         }\r
270         Ext.data.HttpProxy.superclass.destroy.call(this);\r
271     }\r
272 });</pre>
273 </body>
274 </html>