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