PDA

View Full Version : ScriptTagProxy removes function from page



gordonb
10 Oct 2011, 7:55 AM
I could be wrong here, but i think that the afterRequest cleanup on the ScriptTagProxy is a bit keen.
It's removing the javascript callback function from the page with;

delete window[functionName]

In my code, i'm making around 8 calls to my server using the scripttagproxy.
6 of the calls use the same callback function to render the data onto the page.
I experience intermittant results when viewing the page, sometimes all 6 calls get rendered, sometime none, more often somewhere in between.
I believe this to be because the function i use to render my data is being removed.
When i override the afterRequest function with my own that leaves out the offending code all seems to be fine.

Now i'm either doing things the hard way (i'm just trying to make several concurrent requests for cross domain json data, shouldn't be rocket science). But i've tried using Ext.util.JSONP.request, however it only seems capable to making one request at any one time. As my aim is to port the sencha web app into a phonegap ios native app, i need to be using cross domain calls.


function GetReportChartJsonpProxy() {

var store = new Ext.data.Store({
proxy: {
type: 'scripttag',
url: this.reportUrl,
callbackParam: 'ReportCallback',
callbackPrefix: 'callback' + this.reportCarousel.getId(),
scriptIdPrefix: 'injectedScript' + this.reportCarousel.getId(),
timeout: 30,
extraParams: {reportUrl: this.reportUrl,
reportCarouselId: this.reportCarousel.getId()},
listeners: {

PortalDashboard.views.DebugCard.addError('Accessing ' + this.reportUrl + ' resulted in Report Ajax call Error; ');

}
}
}
});

store.proxy.afterRequest = OverriddenStoreAfterRequest;

store.load();

}

function OverriddenStoreAfterRequest() {
return function(request, isLoaded) {
Ext.get(request.scriptId).remove();
clearTimeout(request.timeoutId);

var callbackName = request.stCallback;

if (isLoaded) {
cleanup(callbackName)();
this.lastRequest.completed = true;
} else {
// if we haven't loaded yet, the callback might still be called in the future so don't unset it immediately
window[callbackName] = cleanup(callbackName);
}
};
}






afterRequest: function() {
var cleanup = function(functionName) {
return function() {
window[functionName] = undefined;

try {
delete window[functionName];
} catch(e) {}
};
};

return function(request, isLoaded) {
Ext.get(request.scriptId).remove();
clearTimeout(request.timeoutId);

var callbackName = request.stCallback;

if (isLoaded) {
cleanup(callbackName)();
this.lastRequest.completed = true;
} else {
// if we haven't loaded yet, the callback might still be called in the future so don't unset it immediately
window[callbackName] = cleanup(callbackName);
}
};
}(),