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.2.1
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>
55 * @param {Object} config a configuration object.
57 Ext.data.Connection = function(config){
58 Ext.apply(this, config);
60 <div id="event-Ext.data.Connection-beforerequest"></div>/**
61 * @event beforerequest
62 * Fires before a network request is made to retrieve a data object.
63 * @param {Connection} conn This Connection object.
64 * @param {Object} options The options config object passed to the {@link #request} method.
67 <div id="event-Ext.data.Connection-requestcomplete"></div>/**
68 * @event requestcomplete
69 * Fires if the request was successfully completed.
70 * @param {Connection} conn This Connection object.
71 * @param {Object} response The XHR object containing the response data.
72 * See <a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object</a>
74 * @param {Object} options The options config object passed to the {@link #request} method.
77 <div id="event-Ext.data.Connection-requestexception"></div>/**
78 * @event requestexception
79 * Fires if an error HTTP status was returned from the server.
80 * See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP Status Code Definitions</a>
81 * for details of HTTP status codes.
82 * @param {Connection} conn This Connection object.
83 * @param {Object} response The XHR object containing the response data.
84 * See <a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object</a>
86 * @param {Object} options The options config object passed to the {@link #request} method.
90 Ext.data.Connection.superclass.constructor.call(this);
93 Ext.extend(Ext.data.Connection, Ext.util.Observable, {
94 <div id="cfg-Ext.data.Connection-url"></div>/**
95 * @cfg {String} url (Optional) <p>The default URL to be used for requests to the server. Defaults to undefined.</p>
96 * <p>The <code>url</code> config may be a function which <i>returns</i> the URL to use for the Ajax request. The scope
97 * (<code><b>this</b></code> reference) of the function is the <code>scope</code> option passed to the {@link #request} method.</p>
99 <div id="cfg-Ext.data.Connection-extraParams"></div>/**
100 * @cfg {Object} extraParams (Optional) An object containing properties which are used as
101 * extra parameters to each request made by this object. (defaults to undefined)
103 <div id="cfg-Ext.data.Connection-defaultHeaders"></div>/**
104 * @cfg {Object} defaultHeaders (Optional) An object containing request headers which are added
105 * to each request made by this object. (defaults to undefined)
107 <div id="cfg-Ext.data.Connection-method"></div>/**
108 * @cfg {String} method (Optional) The default HTTP method to be used for requests.
109 * (defaults to undefined; if not set, but {@link #request} params are present, POST will be used;
110 * otherwise, GET will be used.)
112 <div id="cfg-Ext.data.Connection-timeout"></div>/**
113 * @cfg {Number} timeout (Optional) The timeout in milliseconds to be used for requests. (defaults to 30000)
116 <div id="cfg-Ext.data.Connection-autoAbort"></div>/**
117 * @cfg {Boolean} autoAbort (Optional) Whether this request should abort any pending requests. (defaults to false)
122 <div id="cfg-Ext.data.Connection-disableCaching"></div>/**
123 * @cfg {Boolean} disableCaching (Optional) True to add a unique cache-buster param to GET requests. (defaults to true)
126 disableCaching: true,
128 <div id="cfg-Ext.data.Connection-disableCachingParam"></div>/**
129 * @cfg {String} disableCachingParam (Optional) Change the parameter which is sent went disabling caching
130 * through a cache buster. Defaults to '_dc'
133 disableCachingParam: '_dc',
135 <div id="method-Ext.data.Connection-request"></div>/**
136 * <p>Sends an HTTP request to a remote server.</p>
137 * <p><b>Important:</b> Ajax server requests are asynchronous, and this call will
138 * return before the response has been received. Process any returned data
139 * in a callback function.</p>
142 url: 'ajax_demo/sample.json',
143 success: function(response, opts) {
144 var obj = Ext.decode(response.responseText);
147 failure: function(response, opts) {
148 console.log('server-side failure with status code ' + response.status);
152 * <p>To execute a callback function in the correct scope, use the <tt>scope</tt> option.</p>
153 * @param {Object} options An object which may contain the following properties:<ul>
154 * <li><b>url</b> : String/Function (Optional)<div class="sub-desc">The URL to
155 * which to send the request, or a function to call which returns a URL string. The scope of the
156 * function is specified by the <tt>scope</tt> option. Defaults to the configured
157 * <tt>{@link #url}</tt>.</div></li>
158 * <li><b>params</b> : Object/String/Function (Optional)<div class="sub-desc">
159 * An object containing properties which are used as parameters to the
160 * request, a url encoded string or a function to call to get either. The scope of the function
161 * is specified by the <tt>scope</tt> option.</div></li>
162 * <li><b>method</b> : String (Optional)<div class="sub-desc">The HTTP method to use
163 * for the request. Defaults to the configured method, or if no method was configured,
164 * "GET" if no parameters are being sent, and "POST" if parameters are being sent. Note that
165 * the method name is case-sensitive and should be all caps.</div></li>
166 * <li><b>callback</b> : Function (Optional)<div class="sub-desc">The
167 * function to be called upon receipt of the HTTP response. The callback is
168 * called regardless of success or failure and is passed the following
170 * <li><b>options</b> : Object<div class="sub-desc">The parameter to the request call.</div></li>
171 * <li><b>success</b> : Boolean<div class="sub-desc">True if the request succeeded.</div></li>
172 * <li><b>response</b> : Object<div class="sub-desc">The XMLHttpRequest object containing the response data.
173 * See <a href="http://www.w3.org/TR/XMLHttpRequest/">http://www.w3.org/TR/XMLHttpRequest/</a> for details about
174 * accessing elements of the response.</div></li>
176 * <li><a id="request-option-success"></a><b>success</b> : Function (Optional)<div class="sub-desc">The function
177 * to be called upon success of the request. The callback is passed the following
179 * <li><b>response</b> : Object<div class="sub-desc">The XMLHttpRequest object containing the response data.</div></li>
180 * <li><b>options</b> : Object<div class="sub-desc">The parameter to the request call.</div></li>
182 * <li><b>failure</b> : Function (Optional)<div class="sub-desc">The function
183 * to be called upon failure of the request. The callback is passed the
184 * following parameters:<ul>
185 * <li><b>response</b> : Object<div class="sub-desc">The XMLHttpRequest object containing the response data.</div></li>
186 * <li><b>options</b> : Object<div class="sub-desc">The parameter to the request call.</div></li>
188 * <li><b>scope</b> : Object (Optional)<div class="sub-desc">The scope in
189 * which to execute the callbacks: The "this" object for the callback function. If the <tt>url</tt>, or <tt>params</tt> options were
190 * specified as functions from which to draw values, then this also serves as the scope for those function calls.
191 * Defaults to the browser window.</div></li>
192 * <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>
193 * <li><b>form</b> : Element/HTMLElement/String (Optional)<div class="sub-desc">The <tt><form></tt>
194 * Element or the id of the <tt><form></tt> to pull parameters from.</div></li>
195 * <li><a id="request-option-isUpload"></a><b>isUpload</b> : Boolean (Optional)<div class="sub-desc"><b>Only meaningful when used
196 * with the <tt>form</tt> option</b>.
197 * <p>True if the form object is a file upload (will be set automatically if the form was
198 * configured with <b><tt>enctype</tt></b> "multipart/form-data").</p>
199 * <p>File uploads are not performed using normal "Ajax" techniques, that is they are <b>not</b>
200 * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
201 * DOM <tt><form></tt> element temporarily modified to have its
202 * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
203 * to a dynamically generated, hidden <tt><iframe></tt> which is inserted into the document
204 * but removed after the return data has been gathered.</p>
205 * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
206 * server is using JSON to send the return object, then the
207 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
208 * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
209 * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
210 * is created containing a <tt>responseText</tt> property in order to conform to the
211 * requirements of event handlers and callbacks.</p>
212 * <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>
213 * and some server technologies (notably JEE) may require some custom processing in order to
214 * retrieve parameter names and parameter values from the packet content.</p>
216 * <li><b>headers</b> : Object (Optional)<div class="sub-desc">Request
217 * headers to set for the request.</div></li>
218 * <li><b>xmlData</b> : Object (Optional)<div class="sub-desc">XML document
219 * to use for the post. Note: This will be used instead of params for the post
220 * data. Any params will be appended to the URL.</div></li>
221 * <li><b>jsonData</b> : Object/String (Optional)<div class="sub-desc">JSON
222 * data to use as the post. Note: This will be used instead of params for the post
223 * data. Any params will be appended to the URL.</div></li>
224 * <li><b>disableCaching</b> : Boolean (Optional)<div class="sub-desc">True
225 * to add a unique cache-buster param to GET requests.</div></li>
227 * <p>The options object may also contain any other property which might be needed to perform
228 * postprocessing in a callback because it is passed to callback functions.</p>
229 * @return {Number} transactionId The id of the server transaction. This may be used
230 * to cancel the request.
232 request : function(o){
234 if(me.fireEvent(BEFOREREQUEST, me, o)){
236 if(!Ext.isEmpty(o.indicatorText)){
237 me.indicatorText = '<div class="loading-indicator">'+o.indicatorText+"</div>";
239 if(me.indicatorText) {
240 Ext.getDom(o.el).innerHTML = me.indicatorText;
242 o.success = (Ext.isFunction(o.success) ? o.success : function(){}).createInterceptor(function(response) {
243 Ext.getDom(o.el).innerHTML = response.responseText;
248 url = o.url || me.url,
250 cb = {success: me.handleResponse,
251 failure: me.handleFailure,
253 argument: {options: o},
254 timeout : o.timeout || me.timeout
260 if (Ext.isFunction(p)) {
261 p = p.call(o.scope||WINDOW, o);
264 p = Ext.urlEncode(me.extraParams, Ext.isObject(p) ? Ext.urlEncode(p) : p);
266 if (Ext.isFunction(url)) {
267 url = url.call(o.scope || WINDOW, o);
270 if((form = Ext.getDom(o.form))){
271 url = url || form.action;
272 if(o.isUpload || /multipart\/form-data/i.test(form.getAttribute("enctype"))) {
273 return me.doFormUpload.call(me, o, p, url);
275 serForm = Ext.lib.Ajax.serializeForm(form);
276 p = p ? (p + '&' + serForm) : serForm;
279 method = o.method || me.method || ((p || o.xmlData || o.jsonData) ? POST : GET);
281 if(method === GET && (me.disableCaching && o.disableCaching !== false) || o.disableCaching === true){
282 var dcp = o.disableCachingParam || me.disableCachingParam;
283 url = Ext.urlAppend(url, dcp + '=' + (new Date().getTime()));
286 o.headers = Ext.apply(o.headers || {}, me.defaultHeaders || {});
288 if(o.autoAbort === true || me.autoAbort) {
292 if((method == GET || o.xmlData || o.jsonData) && p){
293 url = Ext.urlAppend(url, p);
296 return (me.transId = Ext.lib.Ajax.request(method, url, cb, p, o));
298 return o.callback ? o.callback.apply(o.scope, [o,UNDEFINED,UNDEFINED]) : null;
302 <div id="method-Ext.data.Connection-isLoading"></div>/**
303 * Determine whether this object has a request outstanding.
304 * @param {Number} transactionId (Optional) defaults to the last transaction
305 * @return {Boolean} True if there is an outstanding request.
307 isLoading : function(transId){
308 return transId ? Ext.lib.Ajax.isCallInProgress(transId) : !! this.transId;
311 <div id="method-Ext.data.Connection-abort"></div>/**
312 * Aborts any outstanding request.
313 * @param {Number} transactionId (Optional) defaults to the last transaction
315 abort : function(transId){
316 if(transId || this.isLoading()){
317 Ext.lib.Ajax.abort(transId || this.transId);
322 handleResponse : function(response){
323 this.transId = false;
324 var options = response.argument.options;
325 response.argument = options ? options.argument : null;
326 this.fireEvent(REQUESTCOMPLETE, this, response, options);
328 options.success.call(options.scope, response, options);
330 if(options.callback){
331 options.callback.call(options.scope, options, true, response);
336 handleFailure : function(response, e){
337 this.transId = false;
338 var options = response.argument.options;
339 response.argument = options ? options.argument : null;
340 this.fireEvent(REQUESTEXCEPTION, this, response, options, e);
342 options.failure.call(options.scope, response, options);
344 if(options.callback){
345 options.callback.call(options.scope, options, false, response);
350 doFormUpload : function(o, ps, url){
353 frame = doc.createElement('iframe'),
354 form = Ext.getDom(o.form),
357 encoding = 'multipart/form-data',
361 encoding: form.encoding,
362 enctype: form.enctype,
367 * Originally this behaviour was modified for Opera 10 to apply the secure URL after
368 * the frame had been added to the document. It seems this has since been corrected in
369 * Opera so the behaviour has been reverted, the URL will be set before being added.
375 src: Ext.SSL_SECURE_URL
378 doc.body.appendChild(frame);
380 // This is required so that IE doesn't pop the response up in a new window.
382 document.frames[id].name = id;
391 action: url || buf.action
394 // add dynamic params
395 Ext.iterate(Ext.urlDecode(ps, false), function(k, v){
396 hd = doc.createElement('input');
402 form.appendChild(hd);
408 // bogus response object
409 r = {responseText : '',
411 argument : o.argument},
416 doc = frame.contentWindow.document || frame.contentDocument || WINDOW.frames[id].document;
419 if(/textarea/i.test((firstChild = doc.body.firstChild || {}).tagName)){ // json response wrapped in textarea
420 r.responseText = firstChild.value;
422 r.responseText = doc.body.innerHTML;
425 //in IE the document may still have a body even if returns XML.
426 r.responseXML = doc.XMLDocument || doc;
431 Ext.EventManager.removeListener(frame, LOAD, cb, me);
433 me.fireEvent(REQUESTCOMPLETE, me, r, o);
435 function runCallback(fn, scope, args){
436 if(Ext.isFunction(fn)){
437 fn.apply(scope, args);
441 runCallback(o.success, o.scope, [r, o]);
442 runCallback(o.callback, o.scope, [o, true, r]);
444 if(!me.debugUploads){
445 setTimeout(function(){Ext.removeNode(frame);}, 100);
449 Ext.EventManager.on(frame, LOAD, cb, this);
452 Ext.fly(form).set(buf);
453 Ext.each(hiddens, function(h) {
460 <div id="cls-Ext.Ajax"></div>/**
462 * @extends Ext.data.Connection
463 * <p>The global Ajax request class that provides a simple way to make Ajax requests
464 * with maximum flexibility.</p>
465 * <p>Since Ext.Ajax is a singleton, you can set common properties/events for it once
466 * and override them at the request function level only if necessary.</p>
467 * <p>Common <b>Properties</b> you may want to set are:<div class="mdetail-params"><ul>
468 * <li><b><tt>{@link #method}</tt></b><p class="sub-desc"></p></li>
469 * <li><b><tt>{@link #extraParams}</tt></b><p class="sub-desc"></p></li>
470 * <li><b><tt>{@link #url}</tt></b><p class="sub-desc"></p></li>
473 // Default headers to pass in every request
474 Ext.Ajax.defaultHeaders = {
479 * <p>Common <b>Events</b> you may want to set are:<div class="mdetail-params"><ul>
480 * <li><b><tt>{@link Ext.data.Connection#beforerequest beforerequest}</tt></b><p class="sub-desc"></p></li>
481 * <li><b><tt>{@link Ext.data.Connection#requestcomplete requestcomplete}</tt></b><p class="sub-desc"></p></li>
482 * <li><b><tt>{@link Ext.data.Connection#requestexception requestexception}</tt></b><p class="sub-desc"></p></li>
485 // Example: show a spinner during all Ajax requests
486 Ext.Ajax.on('beforerequest', this.showSpinner, this);
487 Ext.Ajax.on('requestcomplete', this.hideSpinner, this);
488 Ext.Ajax.on('requestexception', this.hideSpinner, this);
491 * <p>An example request:</p>
494 Ext.Ajax.{@link Ext.data.Connection#request request}({
501 params: { foo: 'bar' }
504 // Simple ajax form submission
505 Ext.Ajax.{@link Ext.data.Connection#request request}({
513 Ext.Ajax = new Ext.data.Connection({
514 <div id="cfg-Ext.Ajax-url"></div>/**
515 * @cfg {String} url @hide
517 <div id="cfg-Ext.Ajax-extraParams"></div>/**
518 * @cfg {Object} extraParams @hide
520 <div id="cfg-Ext.Ajax-defaultHeaders"></div>/**
521 * @cfg {Object} defaultHeaders @hide
523 <div id="cfg-Ext.Ajax-method"></div>/**
524 * @cfg {String} method (Optional) @hide
526 <div id="cfg-Ext.Ajax-timeout"></div>/**
527 * @cfg {Number} timeout (Optional) @hide
529 <div id="cfg-Ext.Ajax-autoAbort"></div>/**
530 * @cfg {Boolean} autoAbort (Optional) @hide
533 <div id="cfg-Ext.Ajax-disableCaching"></div>/**
534 * @cfg {Boolean} disableCaching (Optional) @hide
537 <div id="prop-Ext.Ajax-disableCaching"></div>/**
538 * @property disableCaching
539 * True to add a unique cache-buster param to GET requests. (defaults to true)
542 <div id="prop-Ext.Ajax-url"></div>/**
544 * The default URL to be used for requests to the server. (defaults to undefined)
545 * If the server receives all requests through one URL, setting this once is easier than
546 * entering it on every request.
549 <div id="prop-Ext.Ajax-extraParams"></div>/**
550 * @property extraParams
551 * An object containing properties which are used as extra parameters to each request made
552 * by this object (defaults to undefined). Session information and other data that you need
553 * to pass with each request are commonly put here.
556 <div id="prop-Ext.Ajax-defaultHeaders"></div>/**
557 * @property defaultHeaders
558 * An object containing request headers which are added to each request made by this object
559 * (defaults to undefined).
562 <div id="prop-Ext.Ajax-method"></div>/**
564 * The default HTTP method to be used for requests. Note that this is case-sensitive and
565 * should be all caps (defaults to undefined; if not set but params are present will use
566 * <tt>"POST"</tt>, otherwise will use <tt>"GET"</tt>.)
569 <div id="prop-Ext.Ajax-timeout"></div>/**
571 * The timeout in milliseconds to be used for requests. (defaults to 30000)
575 <div id="prop-Ext.Ajax-autoAbort"></div>/**
576 * @property autoAbort
577 * Whether a new request should abort any pending requests. (defaults to false)
582 <div id="method-Ext.Ajax-serializeForm"></div>/**
583 * Serialize the passed form into a url encoded string
584 * @param {String/HTMLElement} form
587 serializeForm : function(form){
588 return Ext.lib.Ajax.serializeForm(form);