4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
15 * @class Roo.data.Connection
16 * @extends Roo.util.Observable
17 * The class encapsulates a connection to the page's originating domain, allowing requests to be made
18 * either to a configured URL, or to a URL specified at request time.
20 * Requests made by this class are asynchronous, and will return immediately. No data from
21 * the server will be available to the statement immediately following the {@link #request} call.
22 * To process returned data, use a callback in the request options object, or an event listener.
24 * Note: If you are doing a file upload, you will not get a normal response object sent back to
25 * your callback or event handler. Since the upload is handled via in IFRAME, there is no XMLHttpRequest.
26 * The response object is created using the innerHTML of the IFRAME's document as the responseText
27 * property and, if present, the IFRAME's XML document as the responseXML property.
29 * This means that a valid XML or HTML document must be returned. If JSON data is required, it is suggested
30 * that it be placed either inside a <textarea> in an HTML document and retrieved from the responseText
31 * using a regex, or inside a CDATA section in an XML document and retrieved from the responseXML using
32 * standard DOM methods.
34 * @param {Object} config a configuration object.
36 Roo.data.Connection = function(config){
37 Roo.apply(this, config);
40 * @event beforerequest
41 * Fires before a network request is made to retrieve a data object.
42 * @param {Connection} conn This Connection object.
43 * @param {Object} options The options config object passed to the {@link #request} method.
45 "beforerequest" : true,
47 * @event requestcomplete
48 * Fires if the request was successfully completed.
49 * @param {Connection} conn This Connection object.
50 * @param {Object} response The XHR object containing the response data.
51 * See {@link http://www.w3.org/TR/XMLHttpRequest/} for details.
52 * @param {Object} options The options config object passed to the {@link #request} method.
54 "requestcomplete" : true,
56 * @event requestexception
57 * Fires if an error HTTP status was returned from the server.
58 * See {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html} for details of HTTP status codes.
59 * @param {Connection} conn This Connection object.
60 * @param {Object} response The XHR object containing the response data.
61 * See {@link http://www.w3.org/TR/XMLHttpRequest/} for details.
62 * @param {Object} options The options config object passed to the {@link #request} method.
64 "requestexception" : true
66 Roo.data.Connection.superclass.constructor.call(this);
69 Roo.extend(Roo.data.Connection, Roo.util.Observable, {
71 * @cfg {String} url (Optional) The default URL to be used for requests to the server. (defaults to undefined)
74 * @cfg {Object} extraParams (Optional) An object containing properties which are used as
75 * extra parameters to each request made by this object. (defaults to undefined)
78 * @cfg {Object} defaultHeaders (Optional) An object containing request headers which are added
79 * to each request made by this object. (defaults to undefined)
82 * @cfg {String} method (Optional) The default HTTP method to be used for requests. (defaults to undefined; if not set but parms are present will use POST, otherwise GET)
85 * @cfg {Number} timeout (Optional) The timeout in milliseconds to be used for requests. (defaults to 30000)
89 * @cfg {Boolean} autoAbort (Optional) Whether this request should abort any pending requests. (defaults to false)
95 * @cfg {Boolean} disableCaching (Optional) True to add a unique cache-buster param to GET requests. (defaults to true)
101 * Sends an HTTP request to a remote server.
102 * @param {Object} options An object which may contain the following properties:<ul>
103 * <li><b>url</b> {String} (Optional) The URL to which to send the request. Defaults to configured URL</li>
104 * <li><b>params</b> {Object/String/Function} (Optional) An object containing properties which are used as parameters to the
105 * request, a url encoded string or a function to call to get either.</li>
106 * <li><b>method</b> {String} (Optional) The HTTP method to use for the request. Defaults to the configured method, or
107 * if no method was configured, "GET" if no parameters are being sent, and "POST" if parameters are being sent.</li>
108 * <li><b>callback</b> {Function} (Optional) The function to be called upon receipt of the HTTP response.
109 * The callback is called regardless of success or failure and is passed the following parameters:<ul>
110 * <li>options {Object} The parameter to the request call.</li>
111 * <li>success {Boolean} True if the request succeeded.</li>
112 * <li>response {Object} The XMLHttpRequest object containing the response data.</li>
114 * <li><b>success</b> {Function} (Optional) The function to be called upon success of the request.
115 * The callback is passed the following parameters:<ul>
116 * <li>response {Object} The XMLHttpRequest object containing the response data.</li>
117 * <li>options {Object} The parameter to the request call.</li>
119 * <li><b>failure</b> {Function} (Optional) The function to be called upon failure of the request.
120 * The callback is passed the following parameters:<ul>
121 * <li>response {Object} The XMLHttpRequest object containing the response data.</li>
122 * <li>options {Object} The parameter to the request call.</li>
124 * <li><b>scope</b> {Object} (Optional) The scope in which to execute the callbacks: The "this" object
125 * for the callback function. Defaults to the browser window.</li>
126 * <li><b>form</b> {Object/String} (Optional) A form object or id to pull parameters from.</li>
127 * <li><b>isUpload</b> {Boolean} (Optional) True if the form object is a file upload (will usually be automatically detected).</li>
128 * <li><b>headers</b> {Object} (Optional) Request headers to set for the request.</li>
129 * <li><b>xmlData</b> {Object} (Optional) XML document to use for the post. Note: This will be used instead of
130 * params for the post data. Any params will be appended to the URL.</li>
131 * <li><b>disableCaching</b> {Boolean} (Optional) True to add a unique cache-buster param to GET requests.</li>
133 * @return {Number} transactionId
135 request : function(o){
136 if(this.fireEvent("beforerequest", this, o) !== false){
139 if(typeof p == "function"){
140 p = p.call(o.scope||window, o);
142 if(typeof p == "object"){
143 p = Roo.urlEncode(o.params);
145 if(this.extraParams){
146 var extras = Roo.urlEncode(this.extraParams);
147 p = p ? (p + '&' + extras) : extras;
150 var url = o.url || this.url;
151 if(typeof url == 'function'){
152 url = url.call(o.scope||window, o);
156 var form = Roo.getDom(o.form);
157 url = url || form.action;
159 var enctype = form.getAttribute("enctype");
162 return this.doFormDataUpload(o, url);
165 if(o.isUpload || (enctype && enctype.toLowerCase() == 'multipart/form-data')){
166 return this.doFormUpload(o, p, url);
168 var f = Roo.lib.Ajax.serializeForm(form);
169 p = p ? (p + '&' + f) : f;
172 if (!o.form && o.formData) {
173 o.formData = o.formData === true ? new FormData() : o.formData;
174 for (var k in o.params) {
175 o.formData.append(k,o.params[k]);
178 return this.doFormDataUpload(o, url);
183 if(this.defaultHeaders){
184 hs = Roo.apply(hs || {}, this.defaultHeaders);
191 success: this.handleResponse,
192 failure: this.handleFailure,
194 argument: {options: o},
195 timeout : o.timeout || this.timeout
198 var method = o.method||this.method||(p ? "POST" : "GET");
200 if(method == 'GET' && (this.disableCaching && o.disableCaching !== false) || o.disableCaching === true){
201 url += (url.indexOf('?') != -1 ? '&' : '?') + '_dc=' + (new Date().getTime());
204 if(typeof o.autoAbort == 'boolean'){ // options gets top priority
208 }else if(this.autoAbort !== false){
212 if((method == 'GET' && p) || o.xmlData){
213 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
216 Roo.lib.Ajax.useDefaultHeader = typeof(o.headers) == 'undefined' || typeof(o.headers['Content-Type']) == 'undefined';
217 this.transId = Roo.lib.Ajax.request(method, url, cb, p, o);
218 Roo.lib.Ajax.useDefaultHeader == true;
221 Roo.callback(o.callback, o.scope, [o, null, null]);
227 * Determine whether this object has a request outstanding.
228 * @param {Number} transactionId (Optional) defaults to the last transaction
229 * @return {Boolean} True if there is an outstanding request.
231 isLoading : function(transId){
233 return Roo.lib.Ajax.isCallInProgress(transId);
235 return this.transId ? true : false;
240 * Aborts any outstanding request.
241 * @param {Number} transactionId (Optional) defaults to the last transaction
243 abort : function(transId){
244 if(transId || this.isLoading()){
245 Roo.lib.Ajax.abort(transId || this.transId);
250 handleResponse : function(response){
251 this.transId = false;
252 var options = response.argument.options;
253 response.argument = options ? options.argument : null;
254 this.fireEvent("requestcomplete", this, response, options);
255 Roo.callback(options.success, options.scope, [response, options]);
256 Roo.callback(options.callback, options.scope, [options, true, response]);
260 handleFailure : function(response, e){
261 this.transId = false;
262 var options = response.argument.options;
263 response.argument = options ? options.argument : null;
264 this.fireEvent("requestexception", this, response, options, e);
265 Roo.callback(options.failure, options.scope, [response, options]);
266 Roo.callback(options.callback, options.scope, [options, false, response]);
270 doFormUpload : function(o, ps, url){
272 var frame = document.createElement('iframe');
275 frame.className = 'x-hidden';
277 frame.src = Roo.SSL_SECURE_URL;
279 document.body.appendChild(frame);
282 document.frames[id].name = id;
285 var form = Roo.getDom(o.form);
287 form.method = 'POST';
288 form.enctype = form.encoding = 'multipart/form-data';
294 if(ps){ // add dynamic params
296 ps = Roo.urlDecode(ps, false);
298 if(ps.hasOwnProperty(k)){
299 hd = document.createElement('input');
303 form.appendChild(hd);
310 var r = { // bogus response object
315 r.argument = o ? o.argument : null;
320 doc = frame.contentWindow.document;
322 doc = (frame.contentDocument || window.frames[id].document);
325 r.responseText = doc.body.innerHTML;
327 if(doc && doc.XMLDocument){
328 r.responseXML = doc.XMLDocument;
337 Roo.EventManager.removeListener(frame, 'load', cb, this);
339 this.fireEvent("requestcomplete", this, r, o);
340 Roo.callback(o.success, o.scope, [r, o]);
341 Roo.callback(o.callback, o.scope, [o, true, r]);
343 setTimeout(function(){document.body.removeChild(frame);}, 100);
346 Roo.EventManager.on(frame, 'load', cb, this);
349 if(hiddens){ // remove dynamic params
350 for(var i = 0, len = hiddens.length; i < len; i++){
351 form.removeChild(hiddens[i]);
355 // this is a 'formdata version???'
358 doFormDataUpload : function(o, url)
362 var form = Roo.getDom(o.form);
363 form.enctype = form.encoding = 'multipart/form-data';
364 formData = o.formData === true ? new FormData(form) : o.formData;
366 formData = o.formData === true ? new FormData() : o.formData;
371 success: this.handleResponse,
372 failure: this.handleFailure,
374 argument: {options: o},
375 timeout : o.timeout || this.timeout
378 if(typeof o.autoAbort == 'boolean'){ // options gets top priority
382 }else if(this.autoAbort !== false){
386 //Roo.lib.Ajax.defaultPostHeader = null;
387 Roo.lib.Ajax.useDefaultHeader = false;
388 this.transId = Roo.lib.Ajax.request( "POST", url, cb, formData, o);
389 Roo.lib.Ajax.useDefaultHeader = true;