Etemplate: Different way to download generated files to avoid destroying sessions of not-visible etemplates

This commit is contained in:
nathangray 2020-09-24 15:18:22 -06:00
parent b5ef5126c6
commit 293f9cc446
2 changed files with 16 additions and 11 deletions

View File

@ -324,14 +324,17 @@ var etemplate2 = /** @class */ (function () {
/** /**
* Download a URL not triggering our unload handler and therefore destroying our et2 request * Download a URL not triggering our unload handler and therefore destroying our et2 request
* *
* We use a new anchor element to avoid not destroying other etemplates as well, which
* is what happens if we use window.location
*
* @param {string} _url * @param {string} _url
*/ */
etemplate2.prototype.download = function (_url) { etemplate2.prototype.download = function (_url) {
// need to unbind unload handler to NOT destroy et2 session var a = document.createElement('a');
this.unbind_unload(); a.href = _url;
document.location = _url; a.download = 'download';
// bind unload handler again (can NOT do it direct, as this would be quick enough to be still triggered!) // Programmatically trigger a click on the anchor element
window.setTimeout(jQuery.proxy(this.bind_unload, this), 100); a.click();
}; };
/** /**
* Loads the template from the given URL and sets the data object * Loads the template from the given URL and sets the data object

View File

@ -414,17 +414,19 @@ export class etemplate2
/** /**
* Download a URL not triggering our unload handler and therefore destroying our et2 request * Download a URL not triggering our unload handler and therefore destroying our et2 request
* *
* We use a new anchor element to avoid not destroying other etemplates as well, which
* is what happens if we use window.location
*
* @param {string} _url * @param {string} _url
*/ */
download(_url) download(_url)
{ {
// need to unbind unload handler to NOT destroy et2 session const a = document.createElement('a');
this.unbind_unload(); a.href = _url;
a.download = 'download';
document.location = _url; // Programmatically trigger a click on the anchor element
a.click();
// bind unload handler again (can NOT do it direct, as this would be quick enough to be still triggered!)
window.setTimeout(jQuery.proxy(this.bind_unload, this), 100);
} }
/** /**