PDA

View Full Version : Dropin replacement for Ext.data.Connection for mime content



jeffiel
3 Apr 2007, 12:57 PM
I need a dropin replacement for when the expected response of a request is a non-text mime type, for example, PDFs or Excel spreadsheets. Since all the usual handling is required, and errors may be thrown, I wanted my usual connection stack to still work.

One caveat, on successful mime document delivery, the callback is not called, b/c firefox does not fire the onload, and does not support onreadystatechange.

If anybody finds it useful, have fun.


Ext.namespace("jpro.data");
jpro.data.Iframe = function(config) {

jpro.data.Iframe.superclass.constructor.call(this, config);

var id = Ext.id();
this.iframe = Ext.DomHelper.append(document.body, {tag: 'iframe', cls: 'hiddenFrame', src: Ext.SSL_SECURE_URL, name: id, id: id}, true);
this.iframe.on("load", this.handleIframeResponse, this);
this.form = Ext.DomHelper.append(document.body, {tag: 'form', target: id }, true);

};
Ext.extend(jpro.data.Iframe, Ext.data.Connection,{


request : function(options){
if(this.fireEvent("beforerequest", this, options) !== false){
var p = options.params;
if(typeof p == "object"){
p = Ext.urlEncode(Ext.apply(options.params, this.extraParams));
}
var cb = {
success: this.handleResponse,
failure: this.handleFailure,
scope: this,
argument: {options: options},
timeout : this.timeout
};
var method = options.method||this.method||(p ? "POST" : "GET");
var url = options.url || this.url;
if(this.autoAbort !== false){
this.abort();
}
if(method == 'GET' && p){
url += (url.indexOf('?') != -1 ? '&' : '?') + p;
p = '';
}

// set callback
Ext.apply(this.iframe, {options: options});

if(method == "POST") {
this.populateForm(p);
this.form.dom.action = url;
this.form.dom.submit();
}
else {
this.iframe.dom.src = url ;
}

}else{
if(typeof options.callback == "function"){
options.callback.call(options.scope||window, options, null, null);
}
}
},

populateForm: function(params) {

// clear any existing elemnets
this.form.update('');

// if param is a string
if(typeof(params) == "string") {
var oparams = [];
params.split('&').each(function(param){

// if amp; then it was encoded, so keep going
if(param.substr(0, 4) == 'amp;') {
oparams[oparams.length-1] += '&' + param;
return;
}

// add to the hash
oparams.push(param);

});

var param;

for(var p=0, len = oparams.length; p<len; p++) {
param = oparams[p].split('=');
Ext.DomHelper.append(this.form, {tag: 'input', type: 'hidden', name: param[0], value: param[1]});
}

}
else for(var p in params)
Ext.DomHelper.append(this.form, {tag: 'input', type: 'hidden', name: p, value: params[p]});

},

handleIframeResponse: function() {

var options = this.iframe.options;
if(!options)
return;

var response = this.iframe.dom.contentDocument;

this.fireEvent("requestcomplete", this, response, options);

if(typeof options.callback == "function"){
options.callback.call(options.scope||window, options, true, {responseXML: response, responseText: this.iframe.dom.innerHTML});
}


}


});