3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataProxy"></div>/**
\r
10 * @class Ext.data.DataProxy
\r
11 * @extends Ext.util.Observable
\r
12 * <p>Abstract base class for implementations which provide retrieval of unformatted data objects.
\r
13 * This class is intended to be extended and should not be created directly. For existing implementations,
\r
14 * see {@link Ext.data.DirectProxy}, {@link Ext.data.HttpProxy}, {@link Ext.data.ScriptTagProxy} and
\r
15 * {@link Ext.data.MemoryProxy}.</p>
\r
16 * <p>DataProxy implementations are usually used in conjunction with an implementation of {@link Ext.data.DataReader}
\r
17 * (of the appropriate type which knows how to parse the data object) to provide a block of
\r
18 * {@link Ext.data.Records} to an {@link Ext.data.Store}.</p>
\r
19 * <p>The parameter to a DataProxy constructor may be an {@link Ext.data.Connection} or can also be the
\r
20 * config object to an {@link Ext.data.Connection}.</p>
\r
21 * <p>Custom implementations must implement either the <code><b>doRequest</b></code> method (preferred) or the
\r
22 * <code>load</code> method (deprecated). See
\r
23 * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#doRequest doRequest} or
\r
24 * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#load load} for additional details.</p>
\r
25 * <p><b><u>Example 1</u></b></p>
\r
27 proxy: new Ext.data.ScriptTagProxy({
\r
28 {@link Ext.data.Connection#url url}: 'http://extjs.com/forum/topics-remote.php'
\r
31 * <p><b><u>Example 2</u></b></p>
\r
33 proxy : new Ext.data.HttpProxy({
\r
34 {@link Ext.data.Connection#method method}: 'GET',
\r
35 {@link Ext.data.HttpProxy#prettyUrls prettyUrls}: false,
\r
36 {@link Ext.data.Connection#url url}: 'local/default.php', // see options parameter for {@link Ext.Ajax#request}
\r
38 // all actions except the following will use above url
\r
39 create : 'local/new.php',
\r
40 update : 'local/update.php'
\r
44 * <p>And <b>new in Ext version 3</b>, attach centralized event-listeners upon the DataProxy class itself! This is a great place
\r
45 * to implement a <i>messaging system</i> to centralize your application's user-feedback and error-handling.</p>
\r
47 // Listen to all "beforewrite" event fired by all proxies.
\r
48 Ext.data.DataProxy.on('beforewrite', function(proxy, action) {
\r
49 console.log('beforewrite: ', action);
\r
52 // Listen to "write" event fired by all proxies
\r
53 Ext.data.DataProxy.on('write', function(proxy, action, data, res, rs) {
\r
54 console.info('write: ', action);
\r
57 // Listen to "exception" event fired by all proxies
\r
58 Ext.data.DataProxy.on('exception', function(proxy, type, action) {
\r
59 console.error(type + action + ' exception);
\r
62 * <b>Note:</b> These three events are all fired with the signature of the corresponding <i>DataProxy instance</i> event {@link #beforewrite beforewrite}, {@link #write write} and {@link #exception exception}.
\r
64 Ext.data.DataProxy = function(conn){
\r
65 // make sure we have a config object here to support ux proxies.
\r
66 // All proxies should now send config into superclass constructor.
\r
69 // This line caused a bug when people use custom Connection object having its own request method.
\r
70 // http://extjs.com/forum/showthread.php?t=67194. Have to set DataProxy config
\r
71 //Ext.applyIf(this, conn);
\r
73 this.api = conn.api;
\r
74 this.url = conn.url;
\r
75 this.restful = conn.restful;
\r
76 this.listeners = conn.listeners;
\r
79 this.prettyUrls = conn.prettyUrls;
\r
81 <div id="cfg-Ext.data.DataProxy-api"></div>/**
\r
83 * Specific urls to call on CRUD action methods "read", "create", "update" and "destroy".
\r
84 * Defaults to:<pre><code>
\r
92 * <p>The url is built based upon the action being executed <tt>[load|create|save|destroy]</tt>
\r
93 * using the commensurate <tt>{@link #api}</tt> property, or if undefined default to the
\r
94 * configured {@link Ext.data.Store}.{@link Ext.data.Store#url url}.</p><br>
\r
95 * <p>For example:</p>
\r
98 load : '/controller/load',
\r
99 create : '/controller/new', // Server MUST return idProperty of new record
\r
100 save : '/controller/update',
\r
101 destroy : '/controller/destroy_action'
\r
104 // Alternatively, one can use the object-form to specify each API-action
\r
106 load: {url: 'read.php', method: 'GET'},
\r
107 create: 'create.php',
\r
108 destroy: 'destroy.php',
\r
112 * <p>If the specific URL for a given CRUD action is undefined, the CRUD action request
\r
113 * will be directed to the configured <tt>{@link Ext.data.Connection#url url}</tt>.</p>
\r
114 * <br><p><b>Note</b>: To modify the URL for an action dynamically the appropriate API
\r
115 * property should be modified before the action is requested using the corresponding before
\r
116 * action event. For example to modify the URL associated with the load action:
\r
118 // modify the url for the action
\r
121 fn: function (store, options) {
\r
122 // use <tt>{@link Ext.data.HttpProxy#setUrl setUrl}</tt> to change the URL for *just* this request.
\r
123 store.proxy.setUrl('changed1.php');
\r
125 // set optional second parameter to true to make this URL change
\r
126 // permanent, applying this URL for all subsequent requests.
\r
127 store.proxy.setUrl('changed1.php', true);
\r
129 // Altering the proxy API should be done using the public
\r
130 // method <tt>{@link Ext.data.DataProxy#setApi setApi}</tt>.
\r
131 store.proxy.setApi('read', 'changed2.php');
\r
133 // Or set the entire API with a config-object.
\r
134 // When using the config-object option, you must redefine the <b>entire</b>
\r
135 // API -- not just a specific action of it.
\r
136 store.proxy.setApi({
\r
137 read : 'changed_read.php',
\r
138 create : 'changed_create.php',
\r
139 update : 'changed_update.php',
\r
140 destroy : 'changed_destroy.php'
\r
150 <div id="event-Ext.data.DataProxy-exception"></div>/**
\r
152 * <p>Fires if an exception occurs in the Proxy during a remote request. This event is relayed
\r
153 * through a corresponding {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},
\r
154 * so any Store instance may observe this event.</p>
\r
155 * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
\r
156 * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of exception events from <b>all</b>
\r
157 * DataProxies by attaching a listener to the Ext.data.Proxy class itself.</p>
\r
158 * <p>This event can be fired for one of two reasons:</p>
\r
159 * <div class="mdetail-params"><ul>
\r
160 * <li>remote-request <b>failed</b> : <div class="sub-desc">
\r
161 * The server did not return status === 200.
\r
163 * <li>remote-request <b>succeeded</b> : <div class="sub-desc">
\r
164 * The remote-request succeeded but the reader could not read the response.
\r
165 * This means the server returned data, but the configured Reader threw an
\r
166 * error while reading the response. In this case, this event will be
\r
167 * raised and the caught error will be passed along into this event.
\r
170 * <br><p>This event fires with two different contexts based upon the 2nd
\r
171 * parameter <tt>type [remote|response]</tt>. The first four parameters
\r
172 * are identical between the two contexts -- only the final two parameters
\r
174 * @param {DataProxy} this The proxy that sent the request
\r
175 * @param {String} type
\r
176 * <p>The value of this parameter will be either <tt>'response'</tt> or <tt>'remote'</tt>.</p>
\r
177 * <div class="mdetail-params"><ul>
\r
178 * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
\r
179 * <p>An <b>invalid</b> response from the server was returned: either 404,
\r
180 * 500 or the response meta-data does not match that defined in the DataReader
\r
181 * (e.g.: root, idProperty, successProperty).</p>
\r
183 * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
\r
184 * <p>A <b>valid</b> response was returned from the server having
\r
185 * successProperty === false. This response might contain an error-message
\r
186 * sent from the server. For example, the user may have failed
\r
187 * authentication/authorization or a database validation error occurred.</p>
\r
190 * @param {String} action Name of the action (see {@link Ext.data.Api#actions}.
\r
191 * @param {Object} options The options for the action that were specified in the {@link #request}.
\r
192 * @param {Object} response
\r
193 * <p>The value of this parameter depends on the value of the <code>type</code> parameter:</p>
\r
194 * <div class="mdetail-params"><ul>
\r
195 * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
\r
196 * <p>The raw browser response object (e.g.: XMLHttpRequest)</p>
\r
198 * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
\r
199 * <p>The decoded response object sent from the server.</p>
\r
202 * @param {Mixed} arg
\r
203 * <p>The type and value of this parameter depends on the value of the <code>type</code> parameter:</p>
\r
204 * <div class="mdetail-params"><ul>
\r
205 * <li><b><tt>'response'</tt></b> : Error<div class="sub-desc">
\r
206 * <p>The JavaScript Error object caught if the configured Reader could not read the data.
\r
207 * If the remote request returns success===false, this parameter will be null.</p>
\r
209 * <li><b><tt>'remote'</tt></b> : Record/Record[]<div class="sub-desc">
\r
210 * <p>This parameter will only exist if the <tt>action</tt> was a <b>write</b> action
\r
211 * (Ext.data.Api.actions.create|update|destroy).</p>
\r
216 <div id="event-Ext.data.DataProxy-beforeload"></div>/**
\r
217 * @event beforeload
\r
218 * Fires before a request to retrieve a data object.
\r
219 * @param {DataProxy} this The proxy for the request
\r
220 * @param {Object} params The params object passed to the {@link #request} function
\r
223 <div id="event-Ext.data.DataProxy-load"></div>/**
\r
225 * Fires before the load method's callback is called.
\r
226 * @param {DataProxy} this The proxy for the request
\r
227 * @param {Object} o The request transaction object
\r
228 * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
\r
231 <div id="event-Ext.data.DataProxy-loadexception"></div>/**
\r
232 * @event loadexception
\r
233 * <p>This event is <b>deprecated</b>. The signature of the loadexception event
\r
234 * varies depending on the proxy, use the catch-all {@link #exception} event instead.
\r
235 * This event will fire in addition to the {@link #exception} event.</p>
\r
236 * @param {misc} misc See {@link #exception}.
\r
240 <div id="event-Ext.data.DataProxy-beforewrite"></div>/**
\r
241 * @event beforewrite
\r
242 * <p>Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy</p>
\r
243 * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
\r
244 * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of beforewrite events from <b>all</b>
\r
245 * DataProxies by attaching a listener to the Ext.data.Proxy class itself.</p>
\r
246 * @param {DataProxy} this The proxy for the request
\r
247 * @param {String} action [Ext.data.Api.actions.create|update|destroy]
\r
248 * @param {Record/Array[Record]} rs The Record(s) to create|update|destroy.
\r
249 * @param {Object} params The request <code>params</code> object. Edit <code>params</code> to add parameters to the request.
\r
252 <div id="event-Ext.data.DataProxy-write"></div>/**
\r
254 * <p>Fires before the request-callback is called</p>
\r
255 * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
\r
256 * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of write events from <b>all</b>
\r
257 * DataProxies by attaching a listener to the Ext.data.Proxy class itself.</p>
\r
258 * @param {DataProxy} this The proxy that sent the request
\r
259 * @param {String} action [Ext.data.Api.actions.create|upate|destroy]
\r
260 * @param {Object} data The data object extracted from the server-response
\r
261 * @param {Object} response The decoded response from server
\r
262 * @param {Record/Record{}} rs The records from Store
\r
263 * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
\r
267 Ext.data.DataProxy.superclass.constructor.call(this);
\r
269 // Prepare the proxy api. Ensures all API-actions are defined with the Object-form.
\r
271 Ext.data.Api.prepare(this);
\r
273 if (e instanceof Ext.data.Api.Error) {
\r
277 // relay each proxy's events onto Ext.data.DataProxy class for centralized Proxy-listening
\r
278 Ext.data.DataProxy.relayEvents(this, ['beforewrite', 'write', 'exception']);
\r
281 Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {
\r
282 <div id="cfg-Ext.data.DataProxy-restful"></div>/**
\r
283 * @cfg {Boolean} restful
\r
284 * <p>Defaults to <tt>false</tt>. Set to <tt>true</tt> to operate in a RESTful manner.</p>
\r
285 * <br><p> Note: this parameter will automatically be set to <tt>true</tt> if the
\r
286 * {@link Ext.data.Store} it is plugged into is set to <code>restful: true</code>. If the
\r
287 * Store is RESTful, there is no need to set this option on the proxy.</p>
\r
288 * <br><p>RESTful implementations enable the serverside framework to automatically route
\r
289 * actions sent to one url based upon the HTTP method, for example:
\r
291 store: new Ext.data.Store({
\r
293 proxy: new Ext.data.HttpProxy({url:'/users'}); // all requests sent to /users
\r
297 * If there is no <code>{@link #api}</code> specified in the configuration of the proxy,
\r
298 * all requests will be marshalled to a single RESTful url (/users) so the serverside
\r
299 * framework can inspect the HTTP Method and act accordingly:
\r
301 <u>Method</u> <u>url</u> <u>action</u>
\r
304 PUT /users/23 update
\r
305 DESTROY /users/23 delete
\r
307 * <p>If set to <tt>true</tt>, a {@link Ext.data.Record#phantom non-phantom} record's
\r
308 * {@link Ext.data.Record#id id} will be appended to the url. Some MVC (e.g., Ruby on Rails,
\r
309 * Merb and Django) support segment based urls where the segments in the URL follow the
\r
310 * Model-View-Controller approach:<pre><code>
\r
311 * someSite.com/controller/action/id
\r
313 * Where the segments in the url are typically:<div class="mdetail-params"><ul>
\r
314 * <li>The first segment : represents the controller class that should be invoked.</li>
\r
315 * <li>The second segment : represents the class function, or method, that should be called.</li>
\r
316 * <li>The third segment : represents the ID (a variable typically passed to the method).</li>
\r
318 * <br><p>Refer to <code>{@link Ext.data.DataProxy#api}</code> for additional information.</p>
\r
322 <div id="method-Ext.data.DataProxy-setApi"></div>/**
\r
323 * <p>Redefines the Proxy's API or a single action of an API. Can be called with two method signatures.</p>
\r
324 * <p>If called with an object as the only parameter, the object should redefine the <b>entire</b> API, e.g.:</p><pre><code>
\r
326 read : '/users/read',
\r
327 create : '/users/create',
\r
328 update : '/users/update',
\r
329 destroy : '/users/destroy'
\r
332 * <p>If called with two parameters, the first parameter should be a string specifying the API action to
\r
333 * redefine and the second parameter should be the URL (or function if using DirectProxy) to call for that action, e.g.:</p><pre><code>
\r
334 proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
\r
336 * @param {String/Object} api An API specification object, or the name of an action.
\r
337 * @param {String/Function} url The URL (or function if using DirectProxy) to call for the action.
\r
339 setApi : function() {
\r
340 if (arguments.length == 1) {
\r
341 var valid = Ext.data.Api.isValid(arguments[0]);
\r
342 if (valid === true) {
\r
343 this.api = arguments[0];
\r
346 throw new Ext.data.Api.Error('invalid', valid);
\r
349 else if (arguments.length == 2) {
\r
350 if (!Ext.data.Api.isAction(arguments[0])) {
\r
351 throw new Ext.data.Api.Error('invalid', arguments[0]);
\r
353 this.api[arguments[0]] = arguments[1];
\r
355 Ext.data.Api.prepare(this);
\r
358 <div id="method-Ext.data.DataProxy-isApiAction"></div>/**
\r
359 * Returns true if the specified action is defined as a unique action in the api-config.
\r
360 * request. If all API-actions are routed to unique urls, the xaction parameter is unecessary. However, if no api is defined
\r
361 * and all Proxy actions are routed to DataProxy#url, the server-side will require the xaction parameter to perform a switch to
\r
362 * the corresponding code for CRUD action.
\r
363 * @param {String [Ext.data.Api.CREATE|READ|UPDATE|DESTROY]} action
\r
364 * @return {Boolean}
\r
366 isApiAction : function(action) {
\r
367 return (this.api[action]) ? true : false;
\r
370 <div id="method-Ext.data.DataProxy-request"></div>/**
\r
371 * All proxy actions are executed through this method. Automatically fires the "before" + action event
\r
372 * @param {String} action Name of the action
\r
373 * @param {Ext.data.Record/Ext.data.Record[]/null} rs Will be null when action is 'load'
\r
374 * @param {Object} params
\r
375 * @param {Ext.data.DataReader} reader
\r
376 * @param {Function} callback
\r
377 * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the Proxy object.
\r
378 * @param {Object} options Any options specified for the action (e.g. see {@link Ext.data.Store#load}.
\r
380 request : function(action, rs, params, reader, callback, scope, options) {
\r
381 if (!this.api[action] && !this.load) {
\r
382 throw new Ext.data.DataProxy.Error('action-undefined', action);
\r
384 params = params || {};
\r
385 if ((action === Ext.data.Api.actions.read) ? this.fireEvent("beforeload", this, params) : this.fireEvent("beforewrite", this, action, rs, params) !== false) {
\r
386 this.doRequest.apply(this, arguments);
\r
389 callback.call(scope || this, null, options, false);
\r
394 <div id="method-Ext.data.DataProxy-load"></div>/**
\r
395 * <b>Deprecated</b> load method using old method signature. See {@doRequest} for preferred method.
\r
397 * @param {Object} params
\r
398 * @param {Object} reader
\r
399 * @param {Object} callback
\r
400 * @param {Object} scope
\r
401 * @param {Object} arg
\r
405 <div id="cfg-Ext.data.DataProxy-doRequest"></div>/**
\r
406 * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers.
\r
407 * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
\r
408 * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
\r
410 doRequest : function(action, rs, params, reader, callback, scope, options) {
\r
411 // default implementation of doRequest for backwards compatibility with 2.0 proxies.
\r
412 // If we're executing here, the action is probably "load".
\r
413 // Call with the pre-3.0 method signature.
\r
414 this.load(params, reader, callback, scope, options);
\r
417 <div id="cfg-Ext.data.DataProxy-onRead"></div>/**
\r
418 * @cfg {Function} onRead Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers. Callback for read {@link Ext.data.Api#actions action}.
\r
419 * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
\r
420 * @param {Object} o The request transaction object
\r
421 * @param {Object} res The server response
\r
422 * @fires loadexception (deprecated)
\r
427 onRead : Ext.emptyFn,
\r
428 <div id="cfg-Ext.data.DataProxy-onWrite"></div>/**
\r
429 * @cfg {Function} onWrite Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers. Callback for <i>create, update and destroy</i> {@link Ext.data.Api#actions actions}.
\r
430 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
431 * @param {Object} trans The request transaction object
\r
432 * @param {Object} res The server response
\r
437 onWrite : Ext.emptyFn,
\r
440 * Sets the appropriate url based upon the action being executed. If restful is true, and only a single record is being acted upon,
\r
441 * url will be built Rails-style, as in "/controller/action/32". restful will aply iff the supplied record is an
\r
442 * instance of Ext.data.Record rather than an Array of them.
\r
443 * @param {String} action The api action being executed [read|create|update|destroy]
\r
444 * @param {Ext.data.Record/Array[Ext.data.Record]} The record or Array of Records being acted upon.
\r
445 * @return {String} url
\r
448 buildUrl : function(action, record) {
\r
449 record = record || null;
\r
451 // conn.url gets nullified after each request. If it's NOT null here, that means the user must have intervened with a call
\r
452 // to DataProxy#setUrl or DataProxy#setApi and changed it before the request was executed. If that's the case, use conn.url,
\r
453 // otherwise, build the url from the api or this.url.
\r
454 var url = (this.conn && this.conn.url) ? this.conn.url : (this.api[action]) ? this.api[action].url : this.url;
\r
456 throw new Ext.data.Api.Error('invalid-url', action);
\r
459 // look for urls having "provides" suffix used in some MVC frameworks like Rails/Merb and others. The provides suffice informs
\r
460 // the server what data-format the client is dealing with and returns data in the same format (eg: application/json, application/xml, etc)
\r
461 // e.g.: /users.json, /users.xml, etc.
\r
462 // with restful routes, we need urls like:
\r
463 // PUT /users/1.json
\r
464 // DELETE /users/1.json
\r
465 var provides = null;
\r
466 var m = url.match(/(.*)(\.json|\.xml|\.html)$/);
\r
468 provides = m[2]; // eg ".json"
\r
469 url = m[1]; // eg: "/users"
\r
471 // prettyUrls is deprectated in favor of restful-config
\r
472 if ((this.restful === true || this.prettyUrls === true) && record instanceof Ext.data.Record && !record.phantom) {
\r
473 url += '/' + record.id;
\r
475 return (provides === null) ? url : url + provides;
\r
478 <div id="method-Ext.data.DataProxy-destroy"></div>/**
\r
479 * Destroys the proxy by purging any event listeners and cancelling any active requests.
\r
481 destroy: function(){
\r
482 this.purgeListeners();
\r
486 // Apply the Observable prototype to the DataProxy class so that proxy instances can relay their
\r
487 // events to the class. Allows for centralized listening of all proxy instances upon the DataProxy class.
\r
488 Ext.apply(Ext.data.DataProxy, Ext.util.Observable.prototype);
\r
489 Ext.util.Observable.call(Ext.data.DataProxy);
\r
491 <div id="cls-Ext.data.DataProxy.Error"></div>/**
\r
492 * @class Ext.data.DataProxy.Error
\r
493 * @extends Ext.Error
\r
494 * DataProxy Error extension.
\r
496 * @param {String} name
\r
497 * @param {Record/Array[Record]/Array}
\r
499 Ext.data.DataProxy.Error = Ext.extend(Ext.Error, {
\r
500 constructor : function(message, arg) {
\r
502 Ext.Error.call(this, message);
\r
504 name: 'Ext.data.DataProxy'
\r
506 Ext.apply(Ext.data.DataProxy.Error.prototype, {
\r
508 'action-undefined': "DataProxy attempted to execute an API-action but found an undefined url / function. Please review your Proxy url/api-configuration.",
\r
509 'api-invalid': 'Recieved an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions from Ext.data.Api.actions.'
\r