4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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-Connection'>/**
19 </span> * The Connection class encapsulates a connection to the page's originating domain, allowing requests to be made either
20 * to a configured URL, or to a URL specified at request time.
22 * Requests made by this class are asynchronous, and will return immediately. No data from the server will be available
23 * to the statement immediately following the {@link #request} call. To process returned data, use a success callback
24 * in the request options object, or an {@link #requestcomplete event listener}.
28 * File uploads are not performed using normal "Ajax" techniques, that is they are not performed using XMLHttpRequests.
29 * Instead the form is submitted in the standard manner with the DOM &lt;form&gt; element temporarily modified to have its
30 * target set to refer to a dynamically generated, hidden &lt;iframe&gt; which is inserted into the document but removed
31 * after the return data has been gathered.
33 * The server response is parsed by the browser to create the document for the IFRAME. If the server is using JSON to
34 * send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to
35 * insert the text unchanged into the document body.
37 * Characters which are significant to an HTML parser must be sent as HTML entities, so encode `<` as `&lt;`, `&` as
40 * The response text is retrieved from the document, and a fake XMLHttpRequest object is created containing a
41 * responseText property in order to conform to the requirements of event handlers and callbacks.
43 * Be aware that file upload packets are sent with the content type multipart/form and some server technologies
44 * (notably JEE) may require some custom processing in order to retrieve parameter names and parameter values from the
47 * Also note that it's not possible to check the response code of the hidden iframe, so the success handler will ALWAYS fire.
49 Ext.define('Ext.data.Connection', {
51 observable: 'Ext.util.Observable'
64 <span id='Ext-data-Connection-cfg-disableCaching'> /**
65 </span> * @cfg {Boolean} disableCaching
66 * True to add a unique cache-buster param to GET requests.
70 <span id='Ext-data-Connection-cfg-withCredentials'> /**
71 </span> * @cfg {Boolean} withCredentials
72 * True to set `withCredentials = true` on the XHR object
74 withCredentials: false,
76 <span id='Ext-data-Connection-cfg-cors'> /**
77 </span> * @cfg {Boolean} cors
78 * True to enable CORS support on the XHR object. Currently the only effect of this option
79 * is to use the XDomainRequest object instead of XMLHttpRequest if the browser is IE8 or above.
83 <span id='Ext-data-Connection-cfg-disableCachingParam'> /**
84 </span> * @cfg {String} disableCachingParam
85 * Change the parameter which is sent went disabling caching through a cache buster.
87 disableCachingParam: '_dc',
89 <span id='Ext-data-Connection-cfg-timeout'> /**
90 </span> * @cfg {Number} timeout
91 * The timeout in milliseconds to be used for requests.
95 <span id='Ext-data-Connection-cfg-extraParams'> /**
96 </span> * @cfg {Object} extraParams
97 * Any parameters to be appended to the request.
100 useDefaultHeader : true,
101 defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
102 useDefaultXhrHeader : true,
103 defaultXhrHeader : 'XMLHttpRequest',
105 constructor : function(config) {
106 config = config || {};
107 Ext.apply(this, config);
110 <span id='Ext-data-Connection-event-beforerequest'> /**
111 </span> * @event beforerequest
112 * Fires before a network request is made to retrieve a data object.
113 * @param {Ext.data.Connection} conn This Connection object.
114 * @param {Object} options The options config object passed to the {@link #request} method.
117 <span id='Ext-data-Connection-event-requestcomplete'> /**
118 </span> * @event requestcomplete
119 * Fires if the request was successfully completed.
120 * @param {Ext.data.Connection} conn This Connection object.
121 * @param {Object} response The XHR object containing the response data.
122 * See [The XMLHttpRequest Object](http://www.w3.org/TR/XMLHttpRequest/) for details.
123 * @param {Object} options The options config object passed to the {@link #request} method.
126 <span id='Ext-data-Connection-event-requestexception'> /**
127 </span> * @event requestexception
128 * Fires if an error HTTP status was returned from the server.
129 * See [HTTP Status Code Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
130 * for details of HTTP status codes.
131 * @param {Ext.data.Connection} conn This Connection object.
132 * @param {Object} response The XHR object containing the response data.
133 * See [The XMLHttpRequest Object](http://www.w3.org/TR/XMLHttpRequest/) for details.
134 * @param {Object} options The options config object passed to the {@link #request} method.
139 this.mixins.observable.constructor.call(this);
142 <span id='Ext-data-Connection-method-request'> /**
143 </span> * Sends an HTTP request to a remote server.
145 * **Important:** Ajax server requests are asynchronous, and this call will
146 * return before the response has been received. Process any returned data
147 * in a callback function.
150 * url: 'ajax_demo/sample.json',
151 * success: function(response, opts) {
152 * var obj = Ext.decode(response.responseText);
155 * failure: function(response, opts) {
156 * console.log('server-side failure with status code ' + response.status);
160 * To execute a callback function in the correct scope, use the `scope` option.
162 * @param {Object} options An object which may contain the following properties:
164 * (The options object may also contain any other property which might be needed to perform
165 * postprocessing in a callback because it is passed to callback functions.)
167 * @param {String/Function} options.url The URL to which to send the request, or a function
168 * to call which returns a URL string. The scope of the function is specified by the `scope` option.
169 * Defaults to the configured `url`.
171 * @param {Object/String/Function} options.params An object containing properties which are
172 * used as parameters to the request, a url encoded string or a function to call to get either. The scope
173 * of the function is specified by the `scope` option.
175 * @param {String} options.method The HTTP method to use
176 * for the request. Defaults to the configured method, or if no method was configured,
177 * "GET" if no parameters are being sent, and "POST" if parameters are being sent. Note that
178 * the method name is case-sensitive and should be all caps.
180 * @param {Function} options.callback The function to be called upon receipt of the HTTP response.
181 * The callback is called regardless of success or failure and is passed the following parameters:
182 * @param {Object} options.callback.options The parameter to the request call.
183 * @param {Boolean} options.callback.success True if the request succeeded.
184 * @param {Object} options.callback.response The XMLHttpRequest object containing the response data.
185 * See [www.w3.org/TR/XMLHttpRequest/](http://www.w3.org/TR/XMLHttpRequest/) for details about
186 * accessing elements of the response.
188 * @param {Function} options.success The function to be called upon success of the request.
189 * The callback is passed the following parameters:
190 * @param {Object} options.success.response The XMLHttpRequest object containing the response data.
191 * @param {Object} options.success.options The parameter to the request call.
193 * @param {Function} options.failure The function to be called upon success of the request.
194 * The callback is passed the following parameters:
195 * @param {Object} options.failure.response The XMLHttpRequest object containing the response data.
196 * @param {Object} options.failure.options The parameter to the request call.
198 * @param {Object} options.scope The scope in which to execute the callbacks: The "this" object for
199 * the callback function. If the `url`, or `params` options were specified as functions from which to
200 * draw values, then this also serves as the scope for those function calls. Defaults to the browser
203 * @param {Number} options.timeout The timeout in milliseconds to be used for this request.
204 * Defaults to 30 seconds.
206 * @param {Ext.Element/HTMLElement/String} options.form The `<form>` Element or the id of the `<form>`
207 * to pull parameters from.
209 * @param {Boolean} options.isUpload **Only meaningful when used with the `form` option.**
211 * True if the form object is a file upload (will be set automatically if the form was configured
212 * with **`enctype`** `"multipart/form-data"`).
214 * File uploads are not performed using normal "Ajax" techniques, that is they are **not**
215 * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
216 * DOM `<form>` element temporarily modified to have its [target][] set to refer to a dynamically
217 * generated, hidden `<iframe>` which is inserted into the document but removed after the return data
220 * The server response is parsed by the browser to create the document for the IFRAME. If the
221 * server is using JSON to send the return object, then the [Content-Type][] header must be set to
222 * "text/html" in order to tell the browser to insert the text unchanged into the document body.
224 * The response text is retrieved from the document, and a fake XMLHttpRequest object is created
225 * containing a `responseText` property in order to conform to the requirements of event handlers
228 * Be aware that file upload packets are sent with the content type [multipart/form][] and some server
229 * technologies (notably JEE) may require some custom processing in order to retrieve parameter names
230 * and parameter values from the packet content.
232 * [target]: http://www.w3.org/TR/REC-html40/present/frames.html#adef-target
233 * [Content-Type]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
234 * [multipart/form]: http://www.faqs.org/rfcs/rfc2388.html
236 * @param {Object} options.headers Request headers to set for the request.
238 * @param {Object} options.xmlData XML document to use for the post. Note: This will be used instead
239 * of params for the post data. Any params will be appended to the URL.
241 * @param {Object/String} options.jsonData JSON data to use as the post. Note: This will be used
242 * instead of params for the post data. Any params will be appended to the URL.
244 * @param {Boolean} options.disableCaching True to add a unique cache-buster param to GET requests.
246 * @param {Boolean} options.withCredentials True to add the withCredentials property to the XHR object
248 * @return {Object} The request object. This may be used to cancel the request.
250 request : function(options) {
251 options = options || {};
253 scope = options.scope || window,
254 username = options.username || me.username,
255 password = options.password || me.password || '',
262 if (me.fireEvent('beforerequest', me, options) !== false) {
264 requestOptions = me.setOptions(options, scope);
266 if (this.isFormUpload(options) === true) {
267 this.upload(options.form, requestOptions.url, requestOptions.data, options);
271 // if autoabort is set, cancel the current transactions
272 if (options.autoAbort === true || me.autoAbort) {
276 // create a connection object
278 if ((options.cors === true || me.cors === true) && Ext.isIE && Ext.ieVersion >= 8) {
279 xhr = new XDomainRequest();
281 xhr = this.getXhrInstance();
284 async = options.async !== false ? (options.async || me.async) : false;
288 xhr.open(requestOptions.method, requestOptions.url, async, username, password);
290 xhr.open(requestOptions.method, requestOptions.url, async);
293 if (options.withCredentials === true || me.withCredentials === true) {
294 xhr.withCredentials = true;
297 headers = me.setupHeaders(xhr, options, requestOptions.data, requestOptions.params);
299 // create the transaction object
301 id: ++Ext.data.Connection.requestId,
306 timeout: setTimeout(function() {
307 request.timedout = true;
309 }, options.timeout || me.timeout)
311 me.requests[request.id] = request;
312 me.latestId = request.id;
313 // bind our statechange listener
315 xhr.onreadystatechange = Ext.Function.bind(me.onStateChange, me, [request]);
318 if ((options.cors === true || me.cors === true) && Ext.isIE && Ext.ieVersion >= 8) {
319 xhr.onload = function() {
320 me.onComplete(request);
324 // start the request!
325 xhr.send(requestOptions.data);
327 return this.onComplete(request);
331 Ext.callback(options.callback, options.scope, [options, undefined, undefined]);
336 <span id='Ext-data-Connection-method-upload'> /**
337 </span> * Uploads a form using a hidden iframe.
338 * @param {String/HTMLElement/Ext.Element} form The form to upload
339 * @param {String} url The url to post to
340 * @param {String} params Any extra parameters to pass
341 * @param {Object} options The initial options
343 upload: function(form, url, params, options) {
344 form = Ext.getDom(form);
345 options = options || {};
348 frame = document.createElement('iframe'),
350 encoding = 'multipart/form-data',
354 encoding: form.encoding,
355 enctype: form.enctype,
360 * Originally this behaviour was modified for Opera 10 to apply the secure URL after
361 * the frame had been added to the document. It seems this has since been corrected in
362 * Opera so the behaviour has been reverted, the URL will be set before being added.
367 cls: Ext.baseCSSPrefix + 'hide-display',
368 src: Ext.SSL_SECURE_URL
371 document.body.appendChild(frame);
373 // This is required so that IE doesn't pop the response up in a new window.
374 if (document.frames) {
375 document.frames[id].name = id;
383 action: url || buf.action
386 // add dynamic params
388 Ext.iterate(Ext.Object.fromQueryString(params), function(name, value){
389 hiddenItem = document.createElement('input');
390 Ext.fly(hiddenItem).set({
395 form.appendChild(hiddenItem);
396 hiddens.push(hiddenItem);
400 Ext.fly(frame).on('load', Ext.Function.bind(this.onUploadComplete, this, [frame, options]), null, {single: true});
403 Ext.fly(form).set(buf);
404 Ext.each(hiddens, function(h) {
409 <span id='Ext-data-Connection-method-onUploadComplete'> /**
411 * Callback handler for the upload function. After we've submitted the form via the iframe this creates a bogus
412 * response object to simulate an XHR and populates its responseText from the now-loaded iframe's document body
413 * (or a textarea inside the body). We then clean up by removing the iframe
415 onUploadComplete: function(frame, options) {
417 // bogus response object
424 doc = frame.contentWindow.document || frame.contentDocument || window.frames[frame.id].document;
427 if (/textarea/i.test((firstChild = doc.body.firstChild || {}).tagName)) { // json response wrapped in textarea
428 response.responseText = firstChild.value;
430 response.responseText = doc.body.innerHTML;
433 //in IE the document may still have a body even if returns XML.
434 response.responseXML = doc.XMLDocument || doc;
439 me.fireEvent('requestcomplete', me, response, options);
441 Ext.callback(options.success, options.scope, [response, options]);
442 Ext.callback(options.callback, options.scope, [options, true, response]);
444 setTimeout(function(){
445 Ext.removeNode(frame);
449 <span id='Ext-data-Connection-method-isFormUpload'> /**
450 </span> * Detects whether the form is intended to be used for an upload.
453 isFormUpload: function(options){
454 var form = this.getForm(options);
456 return (options.isUpload || (/multipart\/form-data/i).test(form.getAttribute('enctype')));
461 <span id='Ext-data-Connection-method-getForm'> /**
462 </span> * Gets the form object from options.
464 * @param {Object} options The request options
465 * @return {HTMLElement} The form, null if not passed
467 getForm: function(options){
468 return Ext.getDom(options.form) || null;
471 <span id='Ext-data-Connection-method-setOptions'> /**
472 </span> * Sets various options such as the url, params for the request
473 * @param {Object} options The initial options
474 * @param {Object} scope The scope to execute in
475 * @return {Object} The params for the request
477 setOptions: function(options, scope){
479 params = options.params || {},
480 extraParams = me.extraParams,
481 urlParams = options.urlParams,
482 url = options.url || me.url,
483 jsonData = options.jsonData,
489 // allow params to be a method that returns the params object
490 if (Ext.isFunction(params)) {
491 params = params.call(scope, options);
494 // allow url to be a method that returns the actual url
495 if (Ext.isFunction(url)) {
496 url = url.call(scope, options);
499 url = this.setupUrl(options, url);
505 msg: 'No URL specified'
510 // check for xml or json data, and make sure json data is encoded
511 data = options.rawData || options.xmlData || jsonData || null;
512 if (jsonData && !Ext.isPrimitive(jsonData)) {
513 data = Ext.encode(data);
516 // make sure params are a url encoded string and include any extraParams if specified
517 if (Ext.isObject(params)) {
518 params = Ext.Object.toQueryString(params);
521 if (Ext.isObject(extraParams)) {
522 extraParams = Ext.Object.toQueryString(extraParams);
525 params = params + ((extraParams) ? ((params) ? '&' : '') + extraParams : '');
527 urlParams = Ext.isObject(urlParams) ? Ext.Object.toQueryString(urlParams) : urlParams;
529 params = this.setupParams(options, params);
531 // decide the proper method for this request
532 method = (options.method || me.method || ((params || data) ? 'POST' : 'GET')).toUpperCase();
533 this.setupMethod(options, method);
536 disableCache = options.disableCaching !== false ? (options.disableCaching || me.disableCaching) : false;
537 // if the method is get append date to prevent caching
538 if (method === 'GET' && disableCache) {
539 url = Ext.urlAppend(url, (options.disableCachingParam || me.disableCachingParam) + '=' + (new Date().getTime()));
542 // if the method is get or there is json/xml data append the params to the url
543 if ((method == 'GET' || data) && params) {
544 url = Ext.urlAppend(url, params);
548 // allow params to be forced into the url
550 url = Ext.urlAppend(url, urlParams);
556 data: data || params || null
560 <span id='Ext-data-Connection-method-setupUrl'> /**
561 </span> * Template method for overriding url
564 * @param {Object} options
565 * @param {String} url
566 * @return {String} The modified url
568 setupUrl: function(options, url){
569 var form = this.getForm(options);
571 url = url || form.action;
577 <span id='Ext-data-Connection-method-setupParams'> /**
578 </span> * Template method for overriding params
581 * @param {Object} options
582 * @param {String} params
583 * @return {String} The modified params
585 setupParams: function(options, params) {
586 var form = this.getForm(options),
588 if (form && !this.isFormUpload(options)) {
589 serializedForm = Ext.Element.serializeForm(form);
590 params = params ? (params + '&' + serializedForm) : serializedForm;
595 <span id='Ext-data-Connection-method-setupMethod'> /**
596 </span> * Template method for overriding method
599 * @param {Object} options
600 * @param {String} method
601 * @return {String} The modified method
603 setupMethod: function(options, method){
604 if (this.isFormUpload(options)) {
610 <span id='Ext-data-Connection-method-setupHeaders'> /**
611 </span> * Setup all the headers for the request
613 * @param {Object} xhr The xhr object
614 * @param {Object} options The options for the request
615 * @param {Object} data The data for the request
616 * @param {Object} params The params for the request
618 setupHeaders: function(xhr, options, data, params){
620 headers = Ext.apply({}, options.headers || {}, me.defaultHeaders || {}),
621 contentType = me.defaultPostHeader,
622 jsonData = options.jsonData,
623 xmlData = options.xmlData,
627 if (!headers['Content-Type'] && (data || params)) {
629 if (options.rawData) {
630 contentType = 'text/plain';
632 if (xmlData && Ext.isDefined(xmlData)) {
633 contentType = 'text/xml';
634 } else if (jsonData && Ext.isDefined(jsonData)) {
635 contentType = 'application/json';
639 headers['Content-Type'] = contentType;
642 if (me.useDefaultXhrHeader && !headers['X-Requested-With']) {
643 headers['X-Requested-With'] = me.defaultXhrHeader;
645 // set up all the request headers on the xhr object
647 for (key in headers) {
648 if (headers.hasOwnProperty(key)) {
649 header = headers[key];
650 xhr.setRequestHeader(key, header);
655 me.fireEvent('exception', key, header);
660 <span id='Ext-data-Connection-property-getXhrInstance'> /**
661 </span> * Creates the appropriate XHR transport for the browser.
664 getXhrInstance: (function(){
665 var options = [function(){
666 return new XMLHttpRequest();
668 return new ActiveXObject('MSXML2.XMLHTTP.3.0');
670 return new ActiveXObject('MSXML2.XMLHTTP');
672 return new ActiveXObject('Microsoft.XMLHTTP');
674 len = options.length,
677 for(; i < len; ++i) {
687 <span id='Ext-data-Connection-method-isLoading'> /**
688 </span> * Determines whether this object has a request outstanding.
689 * @param {Object} [request] Defaults to the last transaction
690 * @return {Boolean} True if there is an outstanding request.
692 isLoading : function(request) {
694 request = this.getLatest();
696 if (!(request && request.xhr)) {
699 // if there is a connection and readyState is not 0 or 4
700 var state = request.xhr.readyState;
701 return !(state === 0 || state == 4);
704 <span id='Ext-data-Connection-method-abort'> /**
705 </span> * Aborts an active request.
706 * @param {Object} [request] Defaults to the last request
708 abort : function(request) {
712 request = me.getLatest();
715 if (request && me.isLoading(request)) {
717 * Clear out the onreadystatechange here, this allows us
718 * greater control, the browser may/may not fire the function
719 * depending on a series of conditions.
721 request.xhr.onreadystatechange = null;
723 me.clearTimeout(request);
724 if (!request.timedout) {
725 request.aborted = true;
727 me.onComplete(request);
732 <span id='Ext-data-Connection-method-abortAll'> /**
733 </span> * Aborts all active requests
735 abortAll: function(){
736 var requests = this.requests,
739 for (id in requests) {
740 if (requests.hasOwnProperty(id)) {
741 this.abort(requests[id]);
746 <span id='Ext-data-Connection-method-getLatest'> /**
747 </span> * Gets the most recent request
749 * @return {Object} The request. Null if there is no recent request
751 getLatest: function(){
752 var id = this.latestId,
756 request = this.requests[id];
758 return request || null;
761 <span id='Ext-data-Connection-method-onStateChange'> /**
762 </span> * Fires when the state of the xhr changes
764 * @param {Object} request The request
766 onStateChange : function(request) {
767 if (request.xhr.readyState == 4) {
768 this.clearTimeout(request);
769 this.onComplete(request);
770 this.cleanup(request);
774 <span id='Ext-data-Connection-method-clearTimeout'> /**
775 </span> * Clears the timeout on the request
777 * @param {Object} The request
779 clearTimeout: function(request){
780 clearTimeout(request.timeout);
781 delete request.timeout;
784 <span id='Ext-data-Connection-method-cleanup'> /**
785 </span> * Cleans up any left over information from the request
787 * @param {Object} The request
789 cleanup: function(request){
794 <span id='Ext-data-Connection-method-onComplete'> /**
795 </span> * To be called when the request has come back from the server
797 * @param {Object} request
798 * @return {Object} The response
800 onComplete : function(request) {
802 options = request.options,
808 result = me.parseStatus(request.xhr.status);
810 // in some browsers we can't access the status if the readyState is not 4, so the request has failed
816 success = result.success;
819 response = me.createResponse(request);
820 me.fireEvent('requestcomplete', me, response, options);
821 Ext.callback(options.success, options.scope, [response, options]);
823 if (result.isException || request.aborted || request.timedout) {
824 response = me.createException(request);
826 response = me.createResponse(request);
828 me.fireEvent('requestexception', me, response, options);
829 Ext.callback(options.failure, options.scope, [response, options]);
831 Ext.callback(options.callback, options.scope, [options, success, response]);
832 delete me.requests[request.id];
836 <span id='Ext-data-Connection-method-parseStatus'> /**
837 </span> * Checks if the response status was successful
838 * @param {Number} status The status code
839 * @return {Object} An object containing success/status state
841 parseStatus: function(status) {
842 // see: https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223
843 status = status == 1223 ? 204 : status;
845 var success = (status >= 200 && status < 300) || status == 304,
862 isException: isException
866 <span id='Ext-data-Connection-method-createResponse'> /**
867 </span> * Creates the response object
869 * @param {Object} request
871 createResponse : function(request) {
872 var xhr = request.xhr,
874 lines = xhr.getAllResponseHeaders ? xhr.getAllResponseHeaders().replace(/\r\n/g, '\n').split('\n') : [],
875 count = lines.length,
876 line, index, key, value, response;
880 index = line.indexOf(':');
882 key = line.substr(0, index).toLowerCase();
883 if (line.charAt(index + 1) == ' ') {
886 headers[key] = line.substr(index + 1);
895 requestId : request.id,
897 statusText : xhr.statusText,
898 getResponseHeader : function(header){ return headers[header.toLowerCase()]; },
899 getAllResponseHeaders : function(){ return headers; },
900 responseText : xhr.responseText,
901 responseXML : xhr.responseXML
904 // If we don't explicitly tear down the xhr reference, IE6/IE7 will hold this in the closure of the
905 // functions created with getResponseHeader/getAllResponseHeaders
910 <span id='Ext-data-Connection-method-createException'> /**
911 </span> * Creates the exception object
913 * @param {Object} request
915 createException : function(request) {
918 requestId : request.id,
919 status : request.aborted ? -1 : 0,
920 statusText : request.aborted ? 'transaction aborted' : 'communication failure',
921 aborted: request.aborted,
922 timedout: request.timedout