4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-proxy-Server'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.data.proxy.Server
21 * @extends Ext.data.proxy.Proxy
23 * <p>ServerProxy is a superclass of {@link Ext.data.proxy.JsonP JsonPProxy} and {@link Ext.data.proxy.Ajax AjaxProxy},
24 * and would not usually be used directly.</p>
26 * <p>ServerProxy should ideally be named HttpProxy as it is a superclass for all HTTP proxies - for Ext JS 4.x it has been
27 * called ServerProxy to enable any 3.x applications that reference the HttpProxy to continue to work (HttpProxy is now an
28 * alias of AjaxProxy).</p>
30 Ext.define('Ext.data.proxy.Server', {
31 extend: 'Ext.data.proxy.Proxy',
32 alias : 'proxy.server',
33 alternateClassName: 'Ext.data.ServerProxy',
34 uses : ['Ext.data.Request'],
36 <span id='Ext-data-proxy-Server-cfg-url'> /**
37 </span> * @cfg {String} url The URL from which to request the data object.
40 <span id='Ext-data-proxy-Server-cfg-reader'> /**
41 </span> * @cfg {Object/String/Ext.data.reader.Reader} reader The Ext.data.reader.Reader to use to decode the server's response. This can
42 * either be a Reader instance, a config object or just a valid Reader type name (e.g. 'json', 'xml').
45 <span id='Ext-data-proxy-Server-cfg-writer'> /**
46 </span> * @cfg {Object/String/Ext.data.writer.Writer} writer The Ext.data.writer.Writer to use to encode any request sent to the server.
47 * This can either be a Writer instance, a config object or just a valid Writer type name (e.g. 'json', 'xml').
50 <span id='Ext-data-proxy-Server-cfg-pageParam'> /**
51 </span> * @cfg {String} pageParam The name of the 'page' parameter to send in a request. Defaults to 'page'. Set this to
52 * undefined if you don't want to send a page parameter
56 <span id='Ext-data-proxy-Server-cfg-startParam'> /**
57 </span> * @cfg {String} startParam The name of the 'start' parameter to send in a request. Defaults to 'start'. Set this
58 * to undefined if you don't want to send a start parameter
62 <span id='Ext-data-proxy-Server-cfg-limitParam'> /**
63 </span> * @cfg {String} limitParam The name of the 'limit' parameter to send in a request. Defaults to 'limit'. Set this
64 * to undefined if you don't want to send a limit parameter
68 <span id='Ext-data-proxy-Server-cfg-groupParam'> /**
69 </span> * @cfg {String} groupParam The name of the 'group' parameter to send in a request. Defaults to 'group'. Set this
70 * to undefined if you don't want to send a group parameter
74 <span id='Ext-data-proxy-Server-cfg-sortParam'> /**
75 </span> * @cfg {String} sortParam The name of the 'sort' parameter to send in a request. Defaults to 'sort'. Set this
76 * to undefined if you don't want to send a sort parameter
80 <span id='Ext-data-proxy-Server-cfg-filterParam'> /**
81 </span> * @cfg {String} filterParam The name of the 'filter' parameter to send in a request. Defaults to 'filter'. Set
82 * this to undefined if you don't want to send a filter parameter
84 filterParam: 'filter',
86 <span id='Ext-data-proxy-Server-cfg-directionParam'> /**
87 </span> * @cfg {String} directionParam The name of the direction parameter to send in a request. <strong>This is only used when simpleSortMode is set to true.</strong>
90 directionParam: 'dir',
92 <span id='Ext-data-proxy-Server-cfg-simpleSortMode'> /**
93 </span> * @cfg {Boolean} simpleSortMode Enabling simpleSortMode in conjunction with remoteSort will only send one sort property and a direction when a remote sort is requested.
94 * The directionParam and sortParam will be sent with the property name and either 'ASC' or 'DESC'
96 simpleSortMode: false,
98 <span id='Ext-data-proxy-Server-cfg-noCache'> /**
99 </span> * @cfg {Boolean} noCache (optional) Defaults to true. Disable caching by adding a unique parameter
100 * name to the request.
104 <span id='Ext-data-proxy-Server-cfg-cacheString'> /**
105 </span> * @cfg {String} cacheString The name of the cache param added to the url when using noCache (defaults to "_dc")
107 cacheString: "_dc",
109 <span id='Ext-data-proxy-Server-cfg-timeout'> /**
110 </span> * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
114 <span id='Ext-data-proxy-Server-cfg-api'> /**
115 </span> * @cfg {Object} api
116 * Specific urls to call on CRUD action methods "read", "create", "update" and "destroy".
117 * Defaults to:<pre><code>
124 * </code></pre>
125 * <p>The url is built based upon the action being executed <tt>[load|create|save|destroy]</tt>
126 * using the commensurate <tt>{@link #api}</tt> property, or if undefined default to the
127 * configured {@link Ext.data.Store}.{@link Ext.data.proxy.Server#url url}.</p><br>
128 * <p>For example:</p>
129 * <pre><code>
131 load : '/controller/load',
132 create : '/controller/new',
133 save : '/controller/update',
134 destroy : '/controller/destroy_action'
136 * </code></pre>
137 * <p>If the specific URL for a given CRUD action is undefined, the CRUD action request
138 * will be directed to the configured <tt>{@link Ext.data.proxy.Server#url url}</tt>.</p>
141 <span id='Ext-data-proxy-Server-method-constructor'> /**
144 constructor: function(config) {
147 config = config || {};
149 <span id='Ext-data-proxy-Server-event-exception'> /**
150 </span> * @event exception
151 * Fires when the server returns an exception
152 * @param {Ext.data.proxy.Proxy} this
153 * @param {Object} response The response from the AJAX request
154 * @param {Ext.data.Operation} operation The operation that triggered request
158 me.callParent([config]);
160 <span id='Ext-data-proxy-Server-cfg-extraParams'> /**
161 </span> * @cfg {Object} extraParams Extra parameters that will be included on every request. Individual requests with params
162 * of the same name will override these params when they are in conflict.
164 me.extraParams = config.extraParams || {};
166 me.api = config.api || {};
168 //backwards compatibility, will be deprecated in 5.0
169 me.nocache = me.noCache;
172 //in a ServerProxy all four CRUD operations are executed in the same manner, so we delegate to doRequest in each case
174 return this.doRequest.apply(this, arguments);
178 return this.doRequest.apply(this, arguments);
182 return this.doRequest.apply(this, arguments);
185 destroy: function() {
186 return this.doRequest.apply(this, arguments);
189 <span id='Ext-data-proxy-Server-method-buildRequest'> /**
190 </span> * Creates and returns an Ext.data.Request object based on the options passed by the {@link Ext.data.Store Store}
191 * that this Proxy is attached to.
192 * @param {Ext.data.Operation} operation The {@link Ext.data.Operation Operation} object to execute
193 * @return {Ext.data.Request} The request object
195 buildRequest: function(operation) {
196 var params = Ext.applyIf(operation.params || {}, this.extraParams || {}),
199 //copy any sorters, filters etc into the params so they can be sent over the wire
200 params = Ext.applyIf(params, this.getParams(params, operation));
202 if (operation.id && !params.id) {
203 params.id = operation.id;
206 request = Ext.create('Ext.data.Request', {
208 action : operation.action,
209 records : operation.records,
210 operation: operation,
214 request.url = this.buildUrl(request);
217 * Save the request on the Operation. Operations don't usually care about Request and Response data, but in the
218 * ServerProxy and any of its subclasses we add both request and response as they may be useful for further processing
220 operation.request = request;
225 <span id='Ext-data-proxy-Server-method-processResponse'> /**
228 processResponse: function(success, operation, request, response, callback, scope){
238 if (success === true) {
239 reader = me.getReader();
240 result = reader.read(me.extractResponseData(response));
241 records = result.records;
242 length = records.length;
244 if (result.success !== false) {
245 mc = Ext.create('Ext.util.MixedCollection', true, function(r) {return r.getId();});
246 mc.addAll(operation.records);
247 for (i = 0; i < length; i++) {
248 record = mc.get(records[i].getId());
252 record.set(record.data);
253 record.endEdit(true);
257 //see comment in buildRequest for why we include the response object here
258 Ext.apply(operation, {
263 operation.setCompleted();
264 operation.setSuccessful();
266 operation.setException(result.message);
267 me.fireEvent('exception', this, response, operation);
270 me.setException(operation, response);
271 me.fireEvent('exception', this, response, operation);
274 //this callback is the one that was passed to the 'read' or 'write' function above
275 if (typeof callback == 'function') {
276 callback.call(scope || me, operation);
279 me.afterRequest(request, success);
282 <span id='Ext-data-proxy-Server-method-setException'> /**
283 </span> * Sets up an exception on the operation
285 * @param {Ext.data.Operation} operation The operation
286 * @param {Object} response The response
288 setException: function(operation, response){
289 operation.setException({
290 status: response.status,
291 statusText: response.statusText
295 <span id='Ext-data-proxy-Server-method-extractResponseData'> /**
296 </span> * Template method to allow subclasses to specify how to get the response for the reader.
298 * @param {Object} response The server response
299 * @return {Mixed} The response data to be used by the reader
301 extractResponseData: function(response){
305 <span id='Ext-data-proxy-Server-method-applyEncoding'> /**
306 </span> * Encode any values being sent to the server. Can be overridden in subclasses.
308 * @param {Array} An array of sorters/filters.
309 * @return {Mixed} The encoded value
311 applyEncoding: function(value){
312 return Ext.encode(value);
315 <span id='Ext-data-proxy-Server-method-encodeSorters'> /**
316 </span> * Encodes the array of {@link Ext.util.Sorter} objects into a string to be sent in the request url. By default,
317 * this simply JSON-encodes the sorter data
318 * @param {Array} sorters The array of {@link Ext.util.Sorter Sorter} objects
319 * @return {String} The encoded sorters
321 encodeSorters: function(sorters) {
323 length = sorters.length,
326 for (; i < length; i++) {
328 property : sorters[i].property,
329 direction: sorters[i].direction
332 return this.applyEncoding(min);
336 <span id='Ext-data-proxy-Server-method-encodeFilters'> /**
337 </span> * Encodes the array of {@link Ext.util.Filter} objects into a string to be sent in the request url. By default,
338 * this simply JSON-encodes the filter data
339 * @param {Array} sorters The array of {@link Ext.util.Filter Filter} objects
340 * @return {String} The encoded filters
342 encodeFilters: function(filters) {
344 length = filters.length,
347 for (; i < length; i++) {
349 property: filters[i].property,
350 value : filters[i].value
353 return this.applyEncoding(min);
356 <span id='Ext-data-proxy-Server-method-getParams'> /**
358 * Copy any sorters, filters etc into the params so they can be sent over the wire
360 getParams: function(params, operation) {
361 params = params || {};
364 isDef = Ext.isDefined,
365 groupers = operation.groupers,
366 sorters = operation.sorters,
367 filters = operation.filters,
368 page = operation.page,
369 start = operation.start,
370 limit = operation.limit,
372 simpleSortMode = me.simpleSortMode,
374 pageParam = me.pageParam,
375 startParam = me.startParam,
376 limitParam = me.limitParam,
377 groupParam = me.groupParam,
378 sortParam = me.sortParam,
379 filterParam = me.filterParam,
380 directionParam = me.directionParam;
382 if (pageParam && isDef(page)) {
383 params[pageParam] = page;
386 if (startParam && isDef(start)) {
387 params[startParam] = start;
390 if (limitParam && isDef(limit)) {
391 params[limitParam] = limit;
394 if (groupParam && groupers && groupers.length > 0) {
395 // Grouper is a subclass of sorter, so we can just use the sorter method
396 params[groupParam] = me.encodeSorters(groupers);
399 if (sortParam && sorters && sorters.length > 0) {
400 if (simpleSortMode) {
401 params[sortParam] = sorters[0].property;
402 params[directionParam] = sorters[0].direction;
404 params[sortParam] = me.encodeSorters(sorters);
409 if (filterParam && filters && filters.length > 0) {
410 params[filterParam] = me.encodeFilters(filters);
416 <span id='Ext-data-proxy-Server-method-buildUrl'> /**
417 </span> * Generates a url based on a given Ext.data.Request object. By default, ServerProxy's buildUrl will
418 * add the cache-buster param to the end of the url. Subclasses may need to perform additional modifications
420 * @param {Ext.data.Request} request The request object
421 * @return {String} The url
423 buildUrl: function(request) {
425 url = me.getUrl(request);
429 Ext.Error.raise("You are using a ServerProxy but have not supplied it with a url.");
434 url = Ext.urlAppend(url, Ext.String.format("{0}={1}", me.cacheString, Ext.Date.now()));
440 <span id='Ext-data-proxy-Server-method-getUrl'> /**
441 </span> * Get the url for the request taking into account the order of priority,
446 * @param {Ext.data.Request} request The request
447 * @return {String} The url
449 getUrl: function(request){
450 return request.url || this.api[request.action] || this.url;
453 <span id='Ext-data-proxy-Server-method-doRequest'> /**
454 </span> * In ServerProxy subclasses, the {@link #create}, {@link #read}, {@link #update} and {@link #destroy} methods all pass
455 * through to doRequest. Each ServerProxy subclass must implement the doRequest method - see {@link Ext.data.proxy.JsonP}
456 * and {@link Ext.data.proxy.Ajax} for examples. This method carries the same signature as each of the methods that delegate to it.
457 * @param {Ext.data.Operation} operation The Ext.data.Operation object
458 * @param {Function} callback The callback function to call when the Operation has completed
459 * @param {Object} scope The scope in which to execute the callback
461 doRequest: function(operation, callback, scope) {
463 Ext.Error.raise("The doRequest function has not been implemented on your Ext.data.proxy.Server subclass. See src/data/ServerProxy.js for details");
467 <span id='Ext-data-proxy-Server-method-afterRequest'> /**
468 </span> * Optional callback function which can be used to clean up after a request has been completed.
469 * @param {Ext.data.Request} request The Request object
470 * @param {Boolean} success True if the request was successful
473 afterRequest: Ext.emptyFn,
475 onDestroy: function() {
476 Ext.destroy(this.reader, this.writer);