3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.data.DataProxy"></div>/**
16 * @class Ext.data.DataProxy
17 * @extends Ext.util.Observable
18 * <p>Abstract base class for implementations which provide retrieval of unformatted data objects.
19 * This class is intended to be extended and should not be created directly. For existing implementations,
20 * see {@link Ext.data.DirectProxy}, {@link Ext.data.HttpProxy}, {@link Ext.data.ScriptTagProxy} and
21 * {@link Ext.data.MemoryProxy}.</p>
22 * <p>DataProxy implementations are usually used in conjunction with an implementation of {@link Ext.data.DataReader}
23 * (of the appropriate type which knows how to parse the data object) to provide a block of
24 * {@link Ext.data.Records} to an {@link Ext.data.Store}.</p>
25 * <p>The parameter to a DataProxy constructor may be an {@link Ext.data.Connection} or can also be the
26 * config object to an {@link Ext.data.Connection}.</p>
27 * <p>Custom implementations must implement either the <code><b>doRequest</b></code> method (preferred) or the
28 * <code>load</code> method (deprecated). See
29 * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#doRequest doRequest} or
30 * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#load load} for additional details.</p>
31 * <p><b><u>Example 1</u></b></p>
33 proxy: new Ext.data.ScriptTagProxy({
34 {@link Ext.data.Connection#url url}: 'http://extjs.com/forum/topics-remote.php'
37 * <p><b><u>Example 2</u></b></p>
39 proxy : new Ext.data.HttpProxy({
40 {@link Ext.data.Connection#method method}: 'GET',
41 {@link Ext.data.HttpProxy#prettyUrls prettyUrls}: false,
42 {@link Ext.data.Connection#url url}: 'local/default.php', // see options parameter for {@link Ext.Ajax#request}
44 // all actions except the following will use above url
45 create : 'local/new.php',
46 update : 'local/update.php'
50 * <p>And <b>new in Ext version 3</b>, attach centralized event-listeners upon the DataProxy class itself! This is a great place
51 * to implement a <i>messaging system</i> to centralize your application's user-feedback and error-handling.</p>
53 // Listen to all "beforewrite" event fired by all proxies.
54 Ext.data.DataProxy.on('beforewrite', function(proxy, action) {
55 console.log('beforewrite: ', action);
58 // Listen to "write" event fired by all proxies
59 Ext.data.DataProxy.on('write', function(proxy, action, data, res, rs) {
60 console.info('write: ', action);
63 // Listen to "exception" event fired by all proxies
64 Ext.data.DataProxy.on('exception', function(proxy, type, action, exception) {
65 console.error(type + action + ' exception);
68 * <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}.
70 Ext.data.DataProxy = function(conn){
71 // make sure we have a config object here to support ux proxies.
72 // All proxies should now send config into superclass constructor.
75 // This line caused a bug when people use custom Connection object having its own request method.
76 // http://extjs.com/forum/showthread.php?t=67194. Have to set DataProxy config
77 //Ext.applyIf(this, conn);
81 this.restful = conn.restful;
82 this.listeners = conn.listeners;
85 this.prettyUrls = conn.prettyUrls;
87 <div id="cfg-Ext.data.DataProxy-api"></div>/**
89 * Specific urls to call on CRUD action methods "read", "create", "update" and "destroy".
90 * Defaults to:<pre><code>
98 * <p>The url is built based upon the action being executed <tt>[load|create|save|destroy]</tt>
99 * using the commensurate <tt>{@link #api}</tt> property, or if undefined default to the
100 * configured {@link Ext.data.Store}.{@link Ext.data.Store#url url}.</p><br>
101 * <p>For example:</p>
104 load : '/controller/load',
105 create : '/controller/new', // Server MUST return idProperty of new record
106 save : '/controller/update',
107 destroy : '/controller/destroy_action'
110 // Alternatively, one can use the object-form to specify each API-action
112 load: {url: 'read.php', method: 'GET'},
113 create: 'create.php',
114 destroy: 'destroy.php',
118 * <p>If the specific URL for a given CRUD action is undefined, the CRUD action request
119 * will be directed to the configured <tt>{@link Ext.data.Connection#url url}</tt>.</p>
120 * <br><p><b>Note</b>: To modify the URL for an action dynamically the appropriate API
121 * property should be modified before the action is requested using the corresponding before
122 * action event. For example to modify the URL associated with the load action:
124 // modify the url for the action
127 fn: function (store, options) {
128 // use <tt>{@link Ext.data.HttpProxy#setUrl setUrl}</tt> to change the URL for *just* this request.
129 store.proxy.setUrl('changed1.php');
131 // set optional second parameter to true to make this URL change
132 // permanent, applying this URL for all subsequent requests.
133 store.proxy.setUrl('changed1.php', true);
135 // Altering the proxy API should be done using the public
136 // method <tt>{@link Ext.data.DataProxy#setApi setApi}</tt>.
137 store.proxy.setApi('read', 'changed2.php');
139 // Or set the entire API with a config-object.
140 // When using the config-object option, you must redefine the <b>entire</b>
141 // API -- not just a specific action of it.
143 read : 'changed_read.php',
144 create : 'changed_create.php',
145 update : 'changed_update.php',
146 destroy : 'changed_destroy.php'
156 <div id="event-Ext.data.DataProxy-exception"></div>/**
158 * <p>Fires if an exception occurs in the Proxy during a remote request. This event is relayed
159 * through a corresponding {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},
160 * so any Store instance may observe this event.</p>
161 * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
162 * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of exception events from <b>all</b>
163 * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.</p>
164 * <p>This event can be fired for one of two reasons:</p>
165 * <div class="mdetail-params"><ul>
166 * <li>remote-request <b>failed</b> : <div class="sub-desc">
167 * The server did not return status === 200.
169 * <li>remote-request <b>succeeded</b> : <div class="sub-desc">
170 * The remote-request succeeded but the reader could not read the response.
171 * This means the server returned data, but the configured Reader threw an
172 * error while reading the response. In this case, this event will be
173 * raised and the caught error will be passed along into this event.
176 * <br><p>This event fires with two different contexts based upon the 2nd
177 * parameter <tt>type [remote|response]</tt>. The first four parameters
178 * are identical between the two contexts -- only the final two parameters
180 * @param {DataProxy} this The proxy that sent the request
181 * @param {String} type
182 * <p>The value of this parameter will be either <tt>'response'</tt> or <tt>'remote'</tt>.</p>
183 * <div class="mdetail-params"><ul>
184 * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
185 * <p>An <b>invalid</b> response from the server was returned: either 404,
186 * 500 or the response meta-data does not match that defined in the DataReader
187 * (e.g.: root, idProperty, successProperty).</p>
189 * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
190 * <p>A <b>valid</b> response was returned from the server having
191 * successProperty === false. This response might contain an error-message
192 * sent from the server. For example, the user may have failed
193 * authentication/authorization or a database validation error occurred.</p>
196 * @param {String} action Name of the action (see {@link Ext.data.Api#actions}.
197 * @param {Object} options The options for the action that were specified in the {@link #request}.
198 * @param {Object} response
199 * <p>The value of this parameter depends on the value of the <code>type</code> parameter:</p>
200 * <div class="mdetail-params"><ul>
201 * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
202 * <p>The raw browser response object (e.g.: XMLHttpRequest)</p>
204 * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
205 * <p>The decoded response object sent from the server.</p>
209 * <p>The type and value of this parameter depends on the value of the <code>type</code> parameter:</p>
210 * <div class="mdetail-params"><ul>
211 * <li><b><tt>'response'</tt></b> : Error<div class="sub-desc">
212 * <p>The JavaScript Error object caught if the configured Reader could not read the data.
213 * If the remote request returns success===false, this parameter will be null.</p>
215 * <li><b><tt>'remote'</tt></b> : Record/Record[]<div class="sub-desc">
216 * <p>This parameter will only exist if the <tt>action</tt> was a <b>write</b> action
217 * (Ext.data.Api.actions.create|update|destroy).</p>
222 <div id="event-Ext.data.DataProxy-beforeload"></div>/**
224 * Fires before a request to retrieve a data object.
225 * @param {DataProxy} this The proxy for the request
226 * @param {Object} params The params object passed to the {@link #request} function
229 <div id="event-Ext.data.DataProxy-load"></div>/**
231 * Fires before the load method's callback is called.
232 * @param {DataProxy} this The proxy for the request
233 * @param {Object} o The request transaction object
234 * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
237 <div id="event-Ext.data.DataProxy-loadexception"></div>/**
238 * @event loadexception
239 * <p>This event is <b>deprecated</b>. The signature of the loadexception event
240 * varies depending on the proxy, use the catch-all {@link #exception} event instead.
241 * This event will fire in addition to the {@link #exception} event.</p>
242 * @param {misc} misc See {@link #exception}.
246 <div id="event-Ext.data.DataProxy-beforewrite"></div>/**
248 * <p>Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy</p>
249 * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
250 * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of beforewrite events from <b>all</b>
251 * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.</p>
252 * @param {DataProxy} this The proxy for the request
253 * @param {String} action [Ext.data.Api.actions.create|update|destroy]
254 * @param {Record/Record[]} rs The Record(s) to create|update|destroy.
255 * @param {Object} params The request <code>params</code> object. Edit <code>params</code> to add parameters to the request.
258 <div id="event-Ext.data.DataProxy-write"></div>/**
260 * <p>Fires before the request-callback is called</p>
261 * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
262 * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of write events from <b>all</b>
263 * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.</p>
264 * @param {DataProxy} this The proxy that sent the request
265 * @param {String} action [Ext.data.Api.actions.create|upate|destroy]
266 * @param {Object} data The data object extracted from the server-response
267 * @param {Object} response The decoded response from server
268 * @param {Record/Record[]} rs The Record(s) from Store
269 * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
273 Ext.data.DataProxy.superclass.constructor.call(this);
275 // Prepare the proxy api. Ensures all API-actions are defined with the Object-form.
277 Ext.data.Api.prepare(this);
279 if (e instanceof Ext.data.Api.Error) {
283 // relay each proxy's events onto Ext.data.DataProxy class for centralized Proxy-listening
284 Ext.data.DataProxy.relayEvents(this, ['beforewrite', 'write', 'exception']);
287 Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {
288 <div id="cfg-Ext.data.DataProxy-restful"></div>/**
289 * @cfg {Boolean} restful
290 * <p>Defaults to <tt>false</tt>. Set to <tt>true</tt> to operate in a RESTful manner.</p>
291 * <br><p> Note: this parameter will automatically be set to <tt>true</tt> if the
292 * {@link Ext.data.Store} it is plugged into is set to <code>restful: true</code>. If the
293 * Store is RESTful, there is no need to set this option on the proxy.</p>
294 * <br><p>RESTful implementations enable the serverside framework to automatically route
295 * actions sent to one url based upon the HTTP method, for example:
297 store: new Ext.data.Store({
299 proxy: new Ext.data.HttpProxy({url:'/users'}); // all requests sent to /users
303 * If there is no <code>{@link #api}</code> specified in the configuration of the proxy,
304 * all requests will be marshalled to a single RESTful url (/users) so the serverside
305 * framework can inspect the HTTP Method and act accordingly:
307 <u>Method</u> <u>url</u> <u>action</u>
311 DESTROY /users/23 delete
313 * <p>If set to <tt>true</tt>, a {@link Ext.data.Record#phantom non-phantom} record's
314 * {@link Ext.data.Record#id id} will be appended to the url. Some MVC (e.g., Ruby on Rails,
315 * Merb and Django) support segment based urls where the segments in the URL follow the
316 * Model-View-Controller approach:<pre><code>
317 * someSite.com/controller/action/id
319 * Where the segments in the url are typically:<div class="mdetail-params"><ul>
320 * <li>The first segment : represents the controller class that should be invoked.</li>
321 * <li>The second segment : represents the class function, or method, that should be called.</li>
322 * <li>The third segment : represents the ID (a variable typically passed to the method).</li>
324 * <br><p>Refer to <code>{@link Ext.data.DataProxy#api}</code> for additional information.</p>
328 <div id="method-Ext.data.DataProxy-setApi"></div>/**
329 * <p>Redefines the Proxy's API or a single action of an API. Can be called with two method signatures.</p>
330 * <p>If called with an object as the only parameter, the object should redefine the <b>entire</b> API, e.g.:</p><pre><code>
332 read : '/users/read',
333 create : '/users/create',
334 update : '/users/update',
335 destroy : '/users/destroy'
338 * <p>If called with two parameters, the first parameter should be a string specifying the API action to
339 * redefine and the second parameter should be the URL (or function if using DirectProxy) to call for that action, e.g.:</p><pre><code>
340 proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
342 * @param {String/Object} api An API specification object, or the name of an action.
343 * @param {String/Function} url The URL (or function if using DirectProxy) to call for the action.
345 setApi : function() {
346 if (arguments.length == 1) {
347 var valid = Ext.data.Api.isValid(arguments[0]);
348 if (valid === true) {
349 this.api = arguments[0];
352 throw new Ext.data.Api.Error('invalid', valid);
355 else if (arguments.length == 2) {
356 if (!Ext.data.Api.isAction(arguments[0])) {
357 throw new Ext.data.Api.Error('invalid', arguments[0]);
359 this.api[arguments[0]] = arguments[1];
361 Ext.data.Api.prepare(this);
364 <div id="method-Ext.data.DataProxy-isApiAction"></div>/**
365 * Returns true if the specified action is defined as a unique action in the api-config.
366 * request. If all API-actions are routed to unique urls, the xaction parameter is unecessary. However, if no api is defined
367 * and all Proxy actions are routed to DataProxy#url, the server-side will require the xaction parameter to perform a switch to
368 * the corresponding code for CRUD action.
369 * @param {String [Ext.data.Api.CREATE|READ|UPDATE|DESTROY]} action
372 isApiAction : function(action) {
373 return (this.api[action]) ? true : false;
376 <div id="method-Ext.data.DataProxy-request"></div>/**
377 * All proxy actions are executed through this method. Automatically fires the "before" + action event
378 * @param {String} action Name of the action
379 * @param {Ext.data.Record/Ext.data.Record[]/null} rs Will be null when action is 'load'
380 * @param {Object} params
381 * @param {Ext.data.DataReader} reader
382 * @param {Function} callback
383 * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the Proxy object.
384 * @param {Object} options Any options specified for the action (e.g. see {@link Ext.data.Store#load}.
386 request : function(action, rs, params, reader, callback, scope, options) {
387 if (!this.api[action] && !this.load) {
388 throw new Ext.data.DataProxy.Error('action-undefined', action);
390 params = params || {};
391 if ((action === Ext.data.Api.actions.read) ? this.fireEvent("beforeload", this, params) : this.fireEvent("beforewrite", this, action, rs, params) !== false) {
392 this.doRequest.apply(this, arguments);
395 callback.call(scope || this, null, options, false);
400 <div id="method-Ext.data.DataProxy-load"></div>/**
401 * <b>Deprecated</b> load method using old method signature. See {@doRequest} for preferred method.
403 * @param {Object} params
404 * @param {Object} reader
405 * @param {Object} callback
406 * @param {Object} scope
407 * @param {Object} arg
411 <div id="cfg-Ext.data.DataProxy-doRequest"></div>/**
412 * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers.
413 * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
414 * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
416 doRequest : function(action, rs, params, reader, callback, scope, options) {
417 // default implementation of doRequest for backwards compatibility with 2.0 proxies.
418 // If we're executing here, the action is probably "load".
419 // Call with the pre-3.0 method signature.
420 this.load(params, reader, callback, scope, options);
423 <div id="cfg-Ext.data.DataProxy-onRead"></div>/**
424 * @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}.
425 * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
426 * @param {Object} o The request transaction object
427 * @param {Object} res The server response
428 * @fires loadexception (deprecated)
433 onRead : Ext.emptyFn,
434 <div id="cfg-Ext.data.DataProxy-onWrite"></div>/**
435 * @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}.
436 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
437 * @param {Object} trans The request transaction object
438 * @param {Object} res The server response
443 onWrite : Ext.emptyFn,
446 * Sets the appropriate url based upon the action being executed. If restful is true, and only a single record is being acted upon,
447 * url will be built Rails-style, as in "/controller/action/32". restful will aply iff the supplied record is an
448 * instance of Ext.data.Record rather than an Array of them.
449 * @param {String} action The api action being executed [read|create|update|destroy]
450 * @param {Ext.data.Record/Ext.data.Record[]} record The record or Array of Records being acted upon.
451 * @return {String} url
454 buildUrl : function(action, record) {
455 record = record || null;
457 // conn.url gets nullified after each request. If it's NOT null here, that means the user must have intervened with a call
458 // to DataProxy#setUrl or DataProxy#setApi and changed it before the request was executed. If that's the case, use conn.url,
459 // otherwise, build the url from the api or this.url.
460 var url = (this.conn && this.conn.url) ? this.conn.url : (this.api[action]) ? this.api[action].url : this.url;
462 throw new Ext.data.Api.Error('invalid-url', action);
465 // look for urls having "provides" suffix used in some MVC frameworks like Rails/Merb and others. The provides suffice informs
466 // the server what data-format the client is dealing with and returns data in the same format (eg: application/json, application/xml, etc)
467 // e.g.: /users.json, /users.xml, etc.
468 // with restful routes, we need urls like:
470 // DELETE /users/1.json
472 var m = url.match(/(.*)(\.json|\.xml|\.html)$/);
474 provides = m[2]; // eg ".json"
475 url = m[1]; // eg: "/users"
477 // prettyUrls is deprectated in favor of restful-config
478 if ((this.restful === true || this.prettyUrls === true) && record instanceof Ext.data.Record && !record.phantom) {
479 url += '/' + record.id;
481 return (provides === null) ? url : url + provides;
484 <div id="method-Ext.data.DataProxy-destroy"></div>/**
485 * Destroys the proxy by purging any event listeners and cancelling any active requests.
488 this.purgeListeners();
492 // Apply the Observable prototype to the DataProxy class so that proxy instances can relay their
493 // events to the class. Allows for centralized listening of all proxy instances upon the DataProxy class.
494 Ext.apply(Ext.data.DataProxy, Ext.util.Observable.prototype);
495 Ext.util.Observable.call(Ext.data.DataProxy);
497 <div id="cls-Ext.data.DataProxy.Error"></div>/**
498 * @class Ext.data.DataProxy.Error
500 * DataProxy Error extension.
502 * @param {String} message Message describing the error.
503 * @param {Record/Record[]} arg
505 Ext.data.DataProxy.Error = Ext.extend(Ext.Error, {
506 constructor : function(message, arg) {
508 Ext.Error.call(this, message);
510 name: 'Ext.data.DataProxy'
512 Ext.apply(Ext.data.DataProxy.Error.prototype, {
514 'action-undefined': "DataProxy attempted to execute an API-action but found an undefined url / function. Please review your Proxy url/api-configuration.",
515 'api-invalid': 'Recieved an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions from Ext.data.Api.actions.'