3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataProxy"></div>/**
\r
9 * @class Ext.data.DataProxy
\r
10 * @extends Ext.util.Observable
\r
11 * <p>Abstract base class for implementations which provide retrieval of unformatted data objects.
\r
12 * This class is intended to be extended and should not be created directly. For existing implementations,
\r
13 * see {@link Ext.data.DirectProxy}, {@link Ext.data.HttpProxy}, {@link Ext.data.ScriptTagProxy} and
\r
14 * {@link Ext.data.MemoryProxy}.</p>
\r
15 * <p>DataProxy implementations are usually used in conjunction with an implementation of {@link Ext.data.DataReader}
\r
16 * (of the appropriate type which knows how to parse the data object) to provide a block of
\r
17 * {@link Ext.data.Records} to an {@link Ext.data.Store}.</p>
\r
18 * <p>The parameter to a DataProxy constructor may be an {@link Ext.data.Connection} or can also be the
\r
19 * config object to an {@link Ext.data.Connection}.</p>
\r
20 * <p>Custom implementations must implement either the <code><b>doRequest</b></code> method (preferred) or the
\r
21 * <code>load</code> method (deprecated). See
\r
22 * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#doRequest doRequest} or
\r
23 * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#load load} for additional details.</p>
\r
24 * <p><b><u>Example 1</u></b></p>
\r
26 proxy: new Ext.data.ScriptTagProxy({
\r
27 {@link Ext.data.Connection#url url}: 'http://extjs.com/forum/topics-remote.php'
\r
30 * <p><b><u>Example 2</u></b></p>
\r
32 proxy : new Ext.data.HttpProxy({
\r
33 {@link Ext.data.Connection#method method}: 'GET',
\r
34 {@link Ext.data.HttpProxy#prettyUrls prettyUrls}: false,
\r
35 {@link Ext.data.Connection#url url}: 'local/default.php', // see options parameter for {@link Ext.Ajax#request}
\r
37 // all actions except the following will use above url
\r
38 create : 'local/new.php',
\r
39 update : 'local/update.php'
\r
44 Ext.data.DataProxy = function(conn){
\r
45 // make sure we have a config object here to support ux proxies.
\r
46 // All proxies should now send config into superclass constructor.
\r
49 // This line caused a bug when people use custom Connection object having its own request method.
\r
50 // http://extjs.com/forum/showthread.php?t=67194. Have to set DataProxy config
\r
51 //Ext.applyIf(this, conn);
\r
53 this.api = conn.api;
\r
54 this.url = conn.url;
\r
55 this.restful = conn.restful;
\r
56 this.listeners = conn.listeners;
\r
59 this.prettyUrls = conn.prettyUrls;
\r
61 <div id="cfg-Ext.data.DataProxy-api"></div>/**
\r
63 * Specific urls to call on CRUD action methods "read", "create", "update" and "destroy".
\r
64 * Defaults to:<pre><code>
\r
72 * <p>If the specific URL for a given CRUD action is undefined, the CRUD action request
\r
73 * will be directed to the configured <tt>{@link Ext.data.Connection#url url}</tt>.</p>
\r
74 * <br><p><b>Note</b>: To modify the URL for an action dynamically the appropriate API
\r
75 * property should be modified before the action is requested using the corresponding before
\r
76 * action event. For example to modify the URL associated with the load action:
\r
78 // modify the url for the action
\r
81 fn: function (store, options) {
\r
82 // use <tt>{@link Ext.data.HttpProxy#setUrl setUrl}</tt> to change the URL for *just* this request.
\r
83 store.proxy.setUrl('changed1.php');
\r
85 // set optional second parameter to true to make this URL change
\r
86 // permanent, applying this URL for all subsequent requests.
\r
87 store.proxy.setUrl('changed1.php', true);
\r
89 // manually set the <b>private</b> connection URL.
\r
90 // <b>Warning:</b> Accessing the private URL property should be avoided.
\r
91 // Use the public method <tt>{@link Ext.data.HttpProxy#setUrl setUrl}</tt> instead, shown above.
\r
92 // It should be noted that changing the URL like this will affect
\r
93 // the URL for just this request. Subsequent requests will use the
\r
94 // API or URL defined in your initial proxy configuration.
\r
95 store.proxy.conn.url = 'changed1.php';
\r
97 // proxy URL will be superseded by API (only if proxy created to use ajax):
\r
98 // It should be noted that proxy API changes are permanent and will
\r
99 // be used for all subsequent requests.
\r
100 store.proxy.api.load = 'changed2.php';
\r
102 // However, altering the proxy API should be done using the public
\r
103 // method <tt>{@link Ext.data.DataProxy#setApi setApi}</tt> instead.
\r
104 store.proxy.setApi('load', 'changed2.php');
\r
106 // Or set the entire API with a config-object.
\r
107 // When using the config-object option, you must redefine the <b>entire</b>
\r
108 // API -- not just a specific action of it.
\r
109 store.proxy.setApi({
\r
110 read : 'changed_read.php',
\r
111 create : 'changed_create.php',
\r
112 update : 'changed_update.php',
\r
113 destroy : 'changed_destroy.php'
\r
121 // Prepare the proxy api. Ensures all API-actions are defined with the Object-form.
\r
123 Ext.data.Api.prepare(this);
\r
125 if (e instanceof Ext.data.Api.Error) {
\r
131 <div id="event-Ext.data.DataProxy-exception"></div>/**
\r
133 * <p>Fires if an exception occurs in the Proxy during a remote request.
\r
134 * This event is relayed through a corresponding
\r
135 * {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},
\r
136 * so any Store instance may observe this event.
\r
137 * This event can be fired for one of two reasons:</p>
\r
138 * <div class="mdetail-params"><ul>
\r
139 * <li>remote-request <b>failed</b> : <div class="sub-desc">
\r
140 * The server did not return status === 200.
\r
142 * <li>remote-request <b>succeeded</b> : <div class="sub-desc">
\r
143 * The remote-request succeeded but the reader could not read the response.
\r
144 * This means the server returned data, but the configured Reader threw an
\r
145 * error while reading the response. In this case, this event will be
\r
146 * raised and the caught error will be passed along into this event.
\r
149 * <br><p>This event fires with two different contexts based upon the 2nd
\r
150 * parameter <tt>type [remote|response]</tt>. The first four parameters
\r
151 * are identical between the two contexts -- only the final two parameters
\r
153 * @param {DataProxy} this The proxy that sent the request
\r
154 * @param {String} type
\r
155 * <p>The value of this parameter will be either <tt>'response'</tt> or <tt>'remote'</tt>.</p>
\r
156 * <div class="mdetail-params"><ul>
\r
157 * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
\r
158 * <p>An <b>invalid</b> response from the server was returned: either 404,
\r
159 * 500 or the response meta-data does not match that defined in the DataReader
\r
160 * (e.g.: root, idProperty, successProperty).</p>
\r
162 * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
\r
163 * <p>A <b>valid</b> response was returned from the server having
\r
164 * successProperty === false. This response might contain an error-message
\r
165 * sent from the server. For example, the user may have failed
\r
166 * authentication/authorization or a database validation error occurred.</p>
\r
169 * @param {String} action Name of the action (see {@link Ext.data.Api#actions}.
\r
170 * @param {Object} options The options for the action that were specified in the {@link #request}.
\r
171 * @param {Object} response
\r
172 * <p>The value of this parameter depends on the value of the <code>type</code> parameter:</p>
\r
173 * <div class="mdetail-params"><ul>
\r
174 * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
\r
175 * <p>The raw browser response object (e.g.: XMLHttpRequest)</p>
\r
177 * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
\r
178 * <p>The decoded response object sent from the server.</p>
\r
181 * @param {Mixed} arg
\r
182 * <p>The type and value of this parameter depends on the value of the <code>type</code> parameter:</p>
\r
183 * <div class="mdetail-params"><ul>
\r
184 * <li><b><tt>'response'</tt></b> : Error<div class="sub-desc">
\r
185 * <p>The JavaScript Error object caught if the configured Reader could not read the data.
\r
186 * If the remote request returns success===false, this parameter will be null.</p>
\r
188 * <li><b><tt>'remote'</tt></b> : Record/Record[]<div class="sub-desc">
\r
189 * <p>This parameter will only exist if the <tt>action</tt> was a <b>write</b> action
\r
190 * (Ext.data.Api.actions.create|update|destroy).</p>
\r
195 <div id="event-Ext.data.DataProxy-beforeload"></div>/**
\r
196 * @event beforeload
\r
197 * Fires before a request to retrieve a data object.
\r
198 * @param {DataProxy} this The proxy for the request
\r
199 * @param {Object} params The params object passed to the {@link #request} function
\r
202 <div id="event-Ext.data.DataProxy-load"></div>/**
\r
204 * Fires before the load method's callback is called.
\r
205 * @param {DataProxy} this The proxy for the request
\r
206 * @param {Object} o The request transaction object
\r
207 * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
\r
210 <div id="event-Ext.data.DataProxy-loadexception"></div>/**
\r
211 * @event loadexception
\r
212 * <p>This event is <b>deprecated</b>. The signature of the loadexception event
\r
213 * varies depending on the proxy, use the catch-all {@link #exception} event instead.
\r
214 * This event will fire in addition to the {@link #exception} event.</p>
\r
215 * @param {misc} misc See {@link #exception}.
\r
219 <div id="event-Ext.data.DataProxy-beforewrite"></div>/**
\r
220 * @event beforewrite
\r
221 * Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy
\r
222 * @param {DataProxy} this The proxy for the request
\r
223 * @param {String} action [Ext.data.Api.actions.create|update|destroy]
\r
224 * @param {Record/Array[Record]} rs The Record(s) to create|update|destroy.
\r
225 * @param {Object} params The request <code>params</code> object. Edit <code>params</code> to add parameters to the request.
\r
228 <div id="event-Ext.data.DataProxy-write"></div>/**
\r
230 * Fires before the request-callback is called
\r
231 * @param {DataProxy} this The proxy that sent the request
\r
232 * @param {String} action [Ext.data.Api.actions.create|upate|destroy]
\r
233 * @param {Object} data The data object extracted from the server-response
\r
234 * @param {Object} response The decoded response from server
\r
235 * @param {Record/Record{}} rs The records from Store
\r
236 * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
\r
240 Ext.data.DataProxy.superclass.constructor.call(this);
\r
243 Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {
\r
244 <div id="cfg-Ext.data.DataProxy-restful"></div>/**
\r
245 * @cfg {Boolean} restful
\r
246 * <p>Defaults to <tt>false</tt>. Set to <tt>true</tt> to operate in a RESTful manner.</p>
\r
247 * <br><p> Note: this parameter will automatically be set to <tt>true</tt> if the
\r
248 * {@link Ext.data.Store} it is plugged into is set to <code>restful: true</code>. If the
\r
249 * Store is RESTful, there is no need to set this option on the proxy.</p>
\r
250 * <br><p>RESTful implementations enable the serverside framework to automatically route
\r
251 * actions sent to one url based upon the HTTP method, for example:
\r
253 store: new Ext.data.Store({
\r
255 proxy: new Ext.data.HttpProxy({url:'/users'}); // all requests sent to /users
\r
259 * There is no <code>{@link #api}</code> specified in the configuration of the proxy,
\r
260 * all requests will be marshalled to a single RESTful url (/users) so the serverside
\r
261 * framework can inspect the HTTP Method and act accordingly:
\r
263 <u>Method</u> <u>url</u> <u>action</u>
\r
266 PUT /users/23 update
\r
267 DESTROY /users/23 delete
\r
272 <div id="method-Ext.data.DataProxy-setApi"></div>/**
\r
273 * <p>Redefines the Proxy's API or a single action of an API. Can be called with two method signatures.</p>
\r
274 * <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
276 read : '/users/read',
\r
277 create : '/users/create',
\r
278 update : '/users/update',
\r
279 destroy : '/users/destroy'
\r
282 * <p>If called with two parameters, the first parameter should be a string specifying the API action to
\r
283 * 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
284 proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
\r
286 * @param {String/Object} api An API specification object, or the name of an action.
\r
287 * @param {String/Function} url The URL (or function if using DirectProxy) to call for the action.
\r
289 setApi : function() {
\r
290 if (arguments.length == 1) {
\r
291 var valid = Ext.data.Api.isValid(arguments[0]);
\r
292 if (valid === true) {
\r
293 this.api = arguments[0];
\r
296 throw new Ext.data.Api.Error('invalid', valid);
\r
299 else if (arguments.length == 2) {
\r
300 if (!Ext.data.Api.isAction(arguments[0])) {
\r
301 throw new Ext.data.Api.Error('invalid', arguments[0]);
\r
303 this.api[arguments[0]] = arguments[1];
\r
305 Ext.data.Api.prepare(this);
\r
308 <div id="method-Ext.data.DataProxy-isApiAction"></div>/**
\r
309 * Returns true if the specified action is defined as a unique action in the api-config.
\r
310 * request. If all API-actions are routed to unique urls, the xaction parameter is unecessary. However, if no api is defined
\r
311 * and all Proxy actions are routed to DataProxy#url, the server-side will require the xaction parameter to perform a switch to
\r
312 * the corresponding code for CRUD action.
\r
313 * @param {String [Ext.data.Api.CREATE|READ|UPDATE|DESTROY]} action
\r
314 * @return {Boolean}
\r
316 isApiAction : function(action) {
\r
317 return (this.api[action]) ? true : false;
\r
320 <div id="method-Ext.data.DataProxy-request"></div>/**
\r
321 * All proxy actions are executed through this method. Automatically fires the "before" + action event
\r
322 * @param {String} action Name of the action
\r
323 * @param {Ext.data.Record/Ext.data.Record[]/null} rs Will be null when action is 'load'
\r
324 * @param {Object} params
\r
325 * @param {Ext.data.DataReader} reader
\r
326 * @param {Function} callback
\r
327 * @param {Object} scope Scope with which to call the callback (defaults to the Proxy object)
\r
328 * @param {Object} options Any options specified for the action (e.g. see {@link Ext.data.Store#load}.
\r
330 request : function(action, rs, params, reader, callback, scope, options) {
\r
331 if (!this.api[action] && !this.load) {
\r
332 throw new Ext.data.DataProxy.Error('action-undefined', action);
\r
334 params = params || {};
\r
335 if ((action === Ext.data.Api.actions.read) ? this.fireEvent("beforeload", this, params) : this.fireEvent("beforewrite", this, action, rs, params) !== false) {
\r
336 this.doRequest.apply(this, arguments);
\r
339 callback.call(scope || this, null, options, false);
\r
344 <div id="method-Ext.data.DataProxy-load"></div>/**
\r
345 * <b>Deprecated</b> load method using old method signature. See {@doRequest} for preferred method.
\r
347 * @param {Object} params
\r
348 * @param {Object} reader
\r
349 * @param {Object} callback
\r
350 * @param {Object} scope
\r
351 * @param {Object} arg
\r
355 <div id="cfg-Ext.data.DataProxy-doRequest"></div>/**
\r
356 * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses
\r
357 * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
\r
358 * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
\r
360 doRequest : function(action, rs, params, reader, callback, scope, options) {
\r
361 // default implementation of doRequest for backwards compatibility with 2.0 proxies.
\r
362 // If we're executing here, the action is probably "load".
\r
363 // Call with the pre-3.0 method signature.
\r
364 this.load(params, reader, callback, scope, options);
\r
369 * Sets the appropriate url based upon the action being executed. If restful is true, and only a single record is being acted upon,
\r
370 * url will be built Rails-style, as in "/controller/action/32". restful will aply iff the supplied record is an
\r
371 * instance of Ext.data.Record rather than an Array of them.
\r
372 * @param {String} action The api action being executed [read|create|update|destroy]
\r
373 * @param {Ext.data.Record/Array[Ext.data.Record]} The record or Array of Records being acted upon.
\r
374 * @return {String} url
\r
377 buildUrl : function(action, record) {
\r
378 record = record || null;
\r
379 var url = (this.api[action]) ? this.api[action].url : this.url;
\r
381 throw new Ext.data.Api.Error('invalid-url', action);
\r
385 var m = url.match(/(.*)(\.\w+)$/); // <-- look for urls with "provides" suffix, e.g.: /users.json, /users.xml, etc
\r
390 // prettyUrls is deprectated in favor of restful-config
\r
391 if ((this.prettyUrls === true || this.restful === true) && record instanceof Ext.data.Record && !record.phantom) {
\r
392 url += '/' + record.id;
\r
394 if (format) { // <-- append the request format if exists (ie: /users/update/69[.json])
\r
400 <div id="method-Ext.data.DataProxy-destroy"></div>/**
\r
401 * Destroys the proxy by purging any event listeners and cancelling any active requests.
\r
403 destroy: function(){
\r
404 this.purgeListeners();
\r
408 <div id="cls-Ext.data.DataProxy.Error"></div>/**
\r
409 * @class Ext.data.DataProxy.Error
\r
410 * @extends Ext.Error
\r
411 * DataProxy Error extension.
\r
413 * @param {String} name
\r
414 * @param {Record/Array[Record]/Array}
\r
416 Ext.data.DataProxy.Error = Ext.extend(Ext.Error, {
\r
417 constructor : function(message, arg) {
\r
419 Ext.Error.call(this, message);
\r
421 name: 'Ext.data.DataProxy'
\r
423 Ext.apply(Ext.data.DataProxy.Error.prototype, {
\r
425 'action-undefined': "DataProxy attempted to execute an API-action but found an undefined url / function. Please review your Proxy url/api-configuration.",
\r
426 'api-invalid': 'Recieved an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions from Ext.data.Api.actions.'
\r