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.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
16 var BEFOREREQUEST = "beforerequest",
17 REQUESTCOMPLETE = "requestcomplete",
18 REQUESTEXCEPTION = "requestexception",
19 UNDEFINED = undefined,
25 <div id="cls-Ext.data.Connection"></div>/**
26 * @class Ext.data.Connection
27 * @extends Ext.util.Observable
28 * <p>The class encapsulates a connection to the page's originating domain, allowing requests to be made
29 * either to a configured URL, or to a URL specified at request time.</p>
30 * <p>Requests made by this class are asynchronous, and will return immediately. No data from
31 * the server will be available to the statement immediately following the {@link #request} call.
32 * To process returned data, use a
33 * <a href="#request-option-success" ext:member="request-option-success" ext:cls="Ext.data.Connection">success callback</a>
34 * in the request options object,
35 * or an {@link #requestcomplete event listener}.</p>
36 * <p><h3>File Uploads</h3><a href="#request-option-isUpload" ext:member="request-option-isUpload" ext:cls="Ext.data.Connection">File uploads</a> are not performed using normal "Ajax" techniques, that
37 * is they are <b>not</b> performed using XMLHttpRequests. Instead the form is submitted in the standard
38 * manner with the DOM <tt><form></tt> element temporarily modified to have its
39 * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
40 * to a dynamically generated, hidden <tt><iframe></tt> which is inserted into the document
41 * but removed after the return data has been gathered.</p>
42 * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
43 * server is using JSON to send the return object, then the
44 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
45 * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
46 * <p>Characters which are significant to an HTML parser must be sent as HTML entities, so encode
47 * "<" as "&lt;", "&" as "&amp;" etc.</p>
48 * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
49 * is created containing a <tt>responseText</tt> property in order to conform to the
50 * requirements of event handlers and callbacks.</p>
51 * <p>Be aware that file upload packets are sent with the content type <a href="http://www.faqs.org/rfcs/rfc2388.html">multipart/form</a>
52 * and some server technologies (notably JEE) may require some custom processing in order to
53 * retrieve parameter names and parameter values from the packet content.</p>
54 * <p>Also note that it's not possible to check the response code of the hidden iframe, so the success handler will ALWAYS fire.</p>
56 * @param {Object} config a configuration object.
58 Ext.data.Connection = function(config){
59 Ext.apply(this, config);
61 <div id="event-Ext.data.Connection-beforerequest"></div>/**
62 * @event beforerequest
63 * Fires before a network request is made to retrieve a data object.
64 * @param {Connection} conn This Connection object.
65 * @param {Object} options The options config object passed to the {@link #request} method.
68 <div id="event-Ext.data.Connection-requestcomplete"></div>/**
69 * @event requestcomplete
70 * Fires if the request was successfully completed.
71 * @param {Connection} conn This Connection object.
72 * @param {Object} response The XHR object containing the response data.
73 * See <a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object</a>
75 * @param {Object} options The options config object passed to the {@link #request} method.
78 <div id="event-Ext.data.Connection-requestexception"></div>/**
79 * @event requestexception
80 * Fires if an error HTTP status was returned from the server.
81 * See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP Status Code Definitions</a>
82 * for details of HTTP status codes.
83 * @param {Connection} conn This Connection object.
84 * @param {Object} response The XHR object containing the response data.
85 * See <a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object</a>
87 * @param {Object} options The options config object passed to the {@link #request} method.
91 Ext.data.Connection.superclass.constructor.call(this);
94 Ext.extend(Ext.data.Connection, Ext.util.Observable, {
95 <div id="cfg-Ext.data.Connection-url"></div>/**
96 * @cfg {String} url (Optional) <p>The default URL to be used for requests to the server. Defaults to undefined.</p>
97 * <p>The <code>url</code> config may be a function which <i>returns</i> the URL to use for the Ajax request. The scope
98 * (<code><b>this</b></code> reference) of the function is the <code>scope</code> option passed to the {@link #request} method.</p>
100 <div id="cfg-Ext.data.Connection-extraParams"></div>/**
101 * @cfg {Object} extraParams (Optional) An object containing properties which are used as
102 * extra parameters to each request made by this object. (defaults to undefined)
104 <div id="cfg-Ext.data.Connection-defaultHeaders"></div>/**
105 * @cfg {Object} defaultHeaders (Optional) An object containing request headers which are added
106 * to each request made by this object. (defaults to undefined)
108 <div id="cfg-Ext.data.Connection-method"></div>/**
109 * @cfg {String} method (Optional) The default HTTP method to be used for requests.
110 * (defaults to undefined; if not set, but {@link #request} params are present, POST will be used;
111 * otherwise, GET will be used.)
113 <div id="cfg-Ext.data.Connection-timeout"></div>/**
114 * @cfg {Number} timeout (Optional) The timeout in milliseconds to be used for requests. (defaults to 30000)
117 <div id="cfg-Ext.data.Connection-autoAbort"></div>/**
118 * @cfg {Boolean} autoAbort (Optional) Whether this request should abort any pending requests. (defaults to false)
123 <div id="cfg-Ext.data.Connection-disableCaching"></div>/**
124 * @cfg {Boolean} disableCaching (Optional) True to add a unique cache-buster param to GET requests. (defaults to true)
127 disableCaching: true,
129 <div id="cfg-Ext.data.Connection-disableCachingParam"></div>/**
130 * @cfg {String} disableCachingParam (Optional) Change the parameter which is sent went disabling caching
131 * through a cache buster. Defaults to '_dc'
134 disableCachingParam: '_dc',
136 <div id="method-Ext.data.Connection-request"></div>/**
137 * <p>Sends an HTTP request to a remote server.</p>
138 * <p><b>Important:</b> Ajax server requests are asynchronous, and this call will
139 * return before the response has been received. Process any returned data
140 * in a callback function.</p>
143 url: 'ajax_demo/sample.json',
144 success: function(response, opts) {
145 var obj = Ext.decode(response.responseText);
148 failure: function(response, opts) {
149 console.log('server-side failure with status code ' + response.status);
153 * <p>To execute a callback function in the correct scope, use the <tt>scope</tt> option.</p>
154 * @param {Object} options An object which may contain the following properties:<ul>
155 * <li><b>url</b> : String/Function (Optional)<div class="sub-desc">The URL to
156 * which to send the request, or a function to call which returns a URL string. The scope of the
157 * function is specified by the <tt>scope</tt> option. Defaults to the configured
158 * <tt>{@link #url}</tt>.</div></li>
159 * <li><b>params</b> : Object/String/Function (Optional)<div class="sub-desc">
160 * An object containing properties which are used as parameters to the
161 * request, a url encoded string or a function to call to get either. The scope of the function
162 * is specified by the <tt>scope</tt> option.</div></li>
163 * <li><b>method</b> : String (Optional)<div class="sub-desc">The HTTP method to use
164 * for the request. Defaults to the configured method, or if no method was configured,
165 * "GET" if no parameters are being sent, and "POST" if parameters are being sent. Note that
166 * the method name is case-sensitive and should be all caps.</div></li>
167 * <li><b>callback</b> : Function (Optional)<div class="sub-desc">The
168 * function to be called upon receipt of the HTTP response. The callback is
169 * called regardless of success or failure and is passed the following
171 * <li><b>options</b> : Object<div class="sub-desc">The parameter to the request call.</div></li>
172 * <li><b>success</b> : Boolean<div class="sub-desc">True if the request succeeded.</div></li>
173 * <li><b>response</b> : Object<div class="sub-desc">The XMLHttpRequest object containing the response data.
174 * See <a href="http://www.w3.org/TR/XMLHttpRequest/">http://www.w3.org/TR/XMLHttpRequest/</a> for details about
175 * accessing elements of the response.</div></li>
177 * <li><a id="request-option-success"></a><b>success</b> : Function (Optional)<div class="sub-desc">The function
178 * to be called upon success of the request. The callback is passed the following
180 * <li><b>response</b> : Object<div class="sub-desc">The XMLHttpRequest object containing the response data.</div></li>
181 * <li><b>options</b> : Object<div class="sub-desc">The parameter to the request call.</div></li>
183 * <li><b>failure</b> : Function (Optional)<div class="sub-desc">The function
184 * to be called upon failure of the request. The callback is passed the
185 * following parameters:<ul>
186 * <li><b>response</b> : Object<div class="sub-desc">The XMLHttpRequest object containing the response data.</div></li>
187 * <li><b>options</b> : Object<div class="sub-desc">The parameter to the request call.</div></li>
189 * <li><b>scope</b> : Object (Optional)<div class="sub-desc">The scope in
190 * which to execute the callbacks: The "this" object for the callback function. If the <tt>url</tt>, or <tt>params</tt> options were
191 * specified as functions from which to draw values, then this also serves as the scope for those function calls.
192 * Defaults to the browser window.</div></li>
193 * <li><b>timeout</b> : Number (Optional)<div class="sub-desc">The timeout in milliseconds to be used for this request. Defaults to 30 seconds.</div></li>
194 * <li><b>form</b> : Element/HTMLElement/String (Optional)<div class="sub-desc">The <tt><form></tt>
195 * Element or the id of the <tt><form></tt> to pull parameters from.</div></li>
196 * <li><a id="request-option-isUpload"></a><b>isUpload</b> : Boolean (Optional)<div class="sub-desc"><b>Only meaningful when used
197 * with the <tt>form</tt> option</b>.
198 * <p>True if the form object is a file upload (will be set automatically if the form was
199 * configured with <b><tt>enctype</tt></b> "multipart/form-data").</p>
200 * <p>File uploads are not performed using normal "Ajax" techniques, that is they are <b>not</b>
201 * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
202 * DOM <tt><form></tt> element temporarily modified to have its
203 * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
204 * to a dynamically generated, hidden <tt><iframe></tt> which is inserted into the document
205 * but removed after the return data has been gathered.</p>
206 * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
207 * server is using JSON to send the return object, then the
208 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
209 * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
210 * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
211 * is created containing a <tt>responseText</tt> property in order to conform to the
212 * requirements of event handlers and callbacks.</p>
213 * <p>Be aware that file upload packets are sent with the content type <a href="http://www.faqs.org/rfcs/rfc2388.html">multipart/form</a>
214 * and some server technologies (notably JEE) may require some custom processing in order to
215 * retrieve parameter names and parameter values from the packet content.</p>
217 * <li><b>headers</b> : Object (Optional)<div class="sub-desc">Request
218 * headers to set for the request.</div></li>
219 * <li><b>xmlData</b> : Object (Optional)<div class="sub-desc">XML document
220 * to use for the post. Note: This will be used instead of params for the post
221 * data. Any params will be appended to the URL.</div></li>
222 * <li><b>jsonData</b> : Object/String (Optional)<div class="sub-desc">JSON
223 * data to use as the post. Note: This will be used instead of params for the post
224 * data. Any params will be appended to the URL.</div></li>
225 * <li><b>disableCaching</b> : Boolean (Optional)<div class="sub-desc">True
226 * to add a unique cache-buster param to GET requests.</div></li>
228 * <p>The options object may also contain any other property which might be needed to perform
229 * postprocessing in a callback because it is passed to callback functions.</p>
230 * @return {Number} transactionId The id of the server transaction. This may be used
231 * to cancel the request.
233 request : function(o){
235 if(me.fireEvent(BEFOREREQUEST, me, o)){
237 if(!Ext.isEmpty(o.indicatorText)){
238 me.indicatorText = '<div class="loading-indicator">'+o.indicatorText+"</div>";
240 if(me.indicatorText) {
241 Ext.getDom(o.el).innerHTML = me.indicatorText;
243 o.success = (Ext.isFunction(o.success) ? o.success : function(){}).createInterceptor(function(response) {
244 Ext.getDom(o.el).innerHTML = response.responseText;
249 url = o.url || me.url,
251 cb = {success: me.handleResponse,
252 failure: me.handleFailure,
254 argument: {options: o},
255 timeout : Ext.num(o.timeout, me.timeout)
261 if (Ext.isFunction(p)) {
262 p = p.call(o.scope||WINDOW, o);
265 p = Ext.urlEncode(me.extraParams, Ext.isObject(p) ? Ext.urlEncode(p) : p);
267 if (Ext.isFunction(url)) {
268 url = url.call(o.scope || WINDOW, o);
271 if((form = Ext.getDom(o.form))){
272 url = url || form.action;
273 if(o.isUpload || (/multipart\/form-data/i.test(form.getAttribute("enctype")))) {
274 return me.doFormUpload.call(me, o, p, url);
276 serForm = Ext.lib.Ajax.serializeForm(form);
277 p = p ? (p + '&' + serForm) : serForm;
280 method = o.method || me.method || ((p || o.xmlData || o.jsonData) ? POST : GET);
282 if(method === GET && (me.disableCaching && o.disableCaching !== false) || o.disableCaching === true){
283 var dcp = o.disableCachingParam || me.disableCachingParam;
284 url = Ext.urlAppend(url, dcp + '=' + (new Date().getTime()));
287 o.headers = Ext.apply(o.headers || {}, me.defaultHeaders || {});
289 if(o.autoAbort === true || me.autoAbort) {
293 if((method == GET || o.xmlData || o.jsonData) && p){
294 url = Ext.urlAppend(url, p);
297 return (me.transId = Ext.lib.Ajax.request(method, url, cb, p, o));
299 return o.callback ? o.callback.apply(o.scope, [o,UNDEFINED,UNDEFINED]) : null;
303 <div id="method-Ext.data.Connection-isLoading"></div>/**
304 * Determine whether this object has a request outstanding.
305 * @param {Number} transactionId (Optional) defaults to the last transaction
306 * @return {Boolean} True if there is an outstanding request.
308 isLoading : function(transId){
309 return transId ? Ext.lib.Ajax.isCallInProgress(transId) : !! this.transId;
312 <div id="method-Ext.data.Connection-abort"></div>/**
313 * Aborts any outstanding request.
314 * @param {Number} transactionId (Optional) defaults to the last transaction
316 abort : function(transId){
317 if(transId || this.isLoading()){
318 Ext.lib.Ajax.abort(transId || this.transId);
323 handleResponse : function(response){
324 this.transId = false;
325 var options = response.argument.options;
326 response.argument = options ? options.argument : null;
327 this.fireEvent(REQUESTCOMPLETE, this, response, options);
329 options.success.call(options.scope, response, options);
331 if(options.callback){
332 options.callback.call(options.scope, options, true, response);
337 handleFailure : function(response, e){
338 this.transId = false;
339 var options = response.argument.options;
340 response.argument = options ? options.argument : null;
341 this.fireEvent(REQUESTEXCEPTION, this, response, options, e);
343 options.failure.call(options.scope, response, options);
345 if(options.callback){
346 options.callback.call(options.scope, options, false, response);
351 doFormUpload : function(o, ps, url){
354 frame = doc.createElement('iframe'),
355 form = Ext.getDom(o.form),
358 encoding = 'multipart/form-data',
362 encoding: form.encoding,
363 enctype: form.enctype,
368 * Originally this behaviour was modified for Opera 10 to apply the secure URL after
369 * the frame had been added to the document. It seems this has since been corrected in
370 * Opera so the behaviour has been reverted, the URL will be set before being added.
376 src: Ext.SSL_SECURE_URL
379 doc.body.appendChild(frame);
381 // This is required so that IE doesn't pop the response up in a new window.
383 document.frames[id].name = id;
392 action: url || buf.action
395 // add dynamic params
396 Ext.iterate(Ext.urlDecode(ps, false), function(k, v){
397 hd = doc.createElement('input');
403 form.appendChild(hd);
409 // bogus response object
410 r = {responseText : '',
412 argument : o.argument},
417 doc = frame.contentWindow.document || frame.contentDocument || WINDOW.frames[id].document;
420 if(/textarea/i.test((firstChild = doc.body.firstChild || {}).tagName)){ // json response wrapped in textarea
421 r.responseText = firstChild.value;
423 r.responseText = doc.body.innerHTML;
426 //in IE the document may still have a body even if returns XML.
427 r.responseXML = doc.XMLDocument || doc;
432 Ext.EventManager.removeListener(frame, LOAD, cb, me);
434 me.fireEvent(REQUESTCOMPLETE, me, r, o);
436 function runCallback(fn, scope, args){
437 if(Ext.isFunction(fn)){
438 fn.apply(scope, args);
442 runCallback(o.success, o.scope, [r, o]);
443 runCallback(o.callback, o.scope, [o, true, r]);
445 if(!me.debugUploads){
446 setTimeout(function(){Ext.removeNode(frame);}, 100);
450 Ext.EventManager.on(frame, LOAD, cb, this);
453 Ext.fly(form).set(buf);
454 Ext.each(hiddens, function(h) {
461 <div id="cls-Ext.Ajax"></div>/**
463 * @extends Ext.data.Connection
464 * <p>The global Ajax request class that provides a simple way to make Ajax requests
465 * with maximum flexibility.</p>
466 * <p>Since Ext.Ajax is a singleton, you can set common properties/events for it once
467 * and override them at the request function level only if necessary.</p>
468 * <p>Common <b>Properties</b> you may want to set are:<div class="mdetail-params"><ul>
469 * <li><b><tt>{@link #method}</tt></b><p class="sub-desc"></p></li>
470 * <li><b><tt>{@link #extraParams}</tt></b><p class="sub-desc"></p></li>
471 * <li><b><tt>{@link #url}</tt></b><p class="sub-desc"></p></li>
474 // Default headers to pass in every request
475 Ext.Ajax.defaultHeaders = {
480 * <p>Common <b>Events</b> you may want to set are:<div class="mdetail-params"><ul>
481 * <li><b><tt>{@link Ext.data.Connection#beforerequest beforerequest}</tt></b><p class="sub-desc"></p></li>
482 * <li><b><tt>{@link Ext.data.Connection#requestcomplete requestcomplete}</tt></b><p class="sub-desc"></p></li>
483 * <li><b><tt>{@link Ext.data.Connection#requestexception requestexception}</tt></b><p class="sub-desc"></p></li>
486 // Example: show a spinner during all Ajax requests
487 Ext.Ajax.on('beforerequest', this.showSpinner, this);
488 Ext.Ajax.on('requestcomplete', this.hideSpinner, this);
489 Ext.Ajax.on('requestexception', this.hideSpinner, this);
492 * <p>An example request:</p>
495 Ext.Ajax.{@link Ext.data.Connection#request request}({
502 params: { foo: 'bar' }
505 // Simple ajax form submission
506 Ext.Ajax.{@link Ext.data.Connection#request request}({
514 Ext.Ajax = new Ext.data.Connection({
515 <div id="cfg-Ext.Ajax-url"></div>/**
516 * @cfg {String} url @hide
518 <div id="cfg-Ext.Ajax-extraParams"></div>/**
519 * @cfg {Object} extraParams @hide
521 <div id="cfg-Ext.Ajax-defaultHeaders"></div>/**
522 * @cfg {Object} defaultHeaders @hide
524 <div id="cfg-Ext.Ajax-method"></div>/**
525 * @cfg {String} method (Optional) @hide
527 <div id="cfg-Ext.Ajax-timeout"></div>/**
528 * @cfg {Number} timeout (Optional) @hide
530 <div id="cfg-Ext.Ajax-autoAbort"></div>/**
531 * @cfg {Boolean} autoAbort (Optional) @hide
534 <div id="cfg-Ext.Ajax-disableCaching"></div>/**
535 * @cfg {Boolean} disableCaching (Optional) @hide
538 <div id="prop-Ext.Ajax-disableCaching"></div>/**
539 * @property disableCaching
540 * True to add a unique cache-buster param to GET requests. (defaults to true)
543 <div id="prop-Ext.Ajax-url"></div>/**
545 * The default URL to be used for requests to the server. (defaults to undefined)
546 * If the server receives all requests through one URL, setting this once is easier than
547 * entering it on every request.
550 <div id="prop-Ext.Ajax-extraParams"></div>/**
551 * @property extraParams
552 * An object containing properties which are used as extra parameters to each request made
553 * by this object (defaults to undefined). Session information and other data that you need
554 * to pass with each request are commonly put here.
557 <div id="prop-Ext.Ajax-defaultHeaders"></div>/**
558 * @property defaultHeaders
559 * An object containing request headers which are added to each request made by this object
560 * (defaults to undefined).
563 <div id="prop-Ext.Ajax-method"></div>/**
565 * The default HTTP method to be used for requests. Note that this is case-sensitive and
566 * should be all caps (defaults to undefined; if not set but params are present will use
567 * <tt>"POST"</tt>, otherwise will use <tt>"GET"</tt>.)
570 <div id="prop-Ext.Ajax-timeout"></div>/**
572 * The timeout in milliseconds to be used for requests. (defaults to 30000)
576 <div id="prop-Ext.Ajax-autoAbort"></div>/**
577 * @property autoAbort
578 * Whether a new request should abort any pending requests. (defaults to false)
583 <div id="method-Ext.Ajax-serializeForm"></div>/**
584 * Serialize the passed form into a url encoded string
585 * @param {String/HTMLElement} form
588 serializeForm : function(form){
589 return Ext.lib.Ajax.serializeForm(form);