From 0203ce5a1f683476e991c7ad9cf7d17c41e3e99b Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Mon, 23 Aug 2021 09:37:18 +0200 Subject: [PATCH] also augmenting Promise returned from egw.request() with an abort method and using that in link-widget --- api/js/etemplate/et2_widget_link.ts | 49 +++++++++++------------------ api/js/jsapi/egw_json.js | 11 ++++++- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/api/js/etemplate/et2_widget_link.ts b/api/js/etemplate/et2_widget_link.ts index 1cb6d92e81..e39a17ad16 100644 --- a/api/js/etemplate/et2_widget_link.ts +++ b/api/js/etemplate/et2_widget_link.ts @@ -639,7 +639,6 @@ export class et2_link_entry extends et2_inputWidget protected search: JQuery; protected clear: JQuery; protected link_button: JQuery; - private response: any; private request: any; private last_search: string; processing: boolean = false; @@ -1190,18 +1189,29 @@ export class et2_link_entry extends et2_inputWidget return response(this.cache[request.term]); } - // Remember callback - this.response = response; - this.search.addClass("loading"); // Remove specific display and revert to CSS file // show() would use inline, should be inline-block this.clear.css('display', ''); - this.request = egw.json("EGroupware\\Api\\Etemplate\\Widget\\Link::ajax_link_search", - [this.app_select.val(), '', request.term, request.options], - this._results, - this, true, this - ).sendRequest(); + + this.request = egw.request("EGroupware\\Api\\Etemplate\\Widget\\Link::ajax_link_search", + [this.app_select.val(), '', request.term, request.options]); + + this.request.then((data) => + { + if (this.request) + { + this.request = null; + } + this.search.removeClass("loading"); + let result = []; + for (var id in data) + { + result.push({"value": id, "label": data[id]}); + } + this.cache[this.search.val()] = result; + response(result); + }); } /** @@ -1246,27 +1256,6 @@ export class et2_link_entry extends et2_inputWidget }, event.data)); } - /** - * Server found some results - * - * @param {Array} data - */ - _results(data) - { - if (this.request) - { - this.request = null; - } - this.search.removeClass("loading"); - var result = []; - for (var id in data) - { - result.push({"value": id, "label": data[id]}); - } - this.cache[this.search.val()] = result; - this.response(result); - } - /** * Create a link using the current internal values * diff --git a/api/js/jsapi/egw_json.js b/api/js/jsapi/egw_json.js index bb4824c3af..3f12ed8cc8 100644 --- a/api/js/jsapi/egw_json.js +++ b/api/js/jsapi/egw_json.js @@ -189,6 +189,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) * @param {function} error option error callback(_xmlhttp, _err) used instead our default this.error * * @return {Promise|boolean} Promise or for async==="keepalive" boolean is returned + * Promise.abort() allows to abort the pending request */ json_request.prototype.sendRequest = function(async, method, error) { @@ -502,11 +503,13 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) * @param {any[]} _parameters * * @return Promise resolving to data part (not full response, which can contain other parts) + * Promise.abort() allows to abort the pending request */ request: function(_menuaction, _parameters) { const request = new json_request(_menuaction, _parameters, null, this, true, this, this); - return request.sendRequest().then(function(response) + const response = request.sendRequest(); + let promise = response.then(function(response) { // The ajax request has completed, get just the data & pass it on if(response && response.response) @@ -527,6 +530,12 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) } return undefined; }); + // pass abort method to returned response + if (typeof response.abort === 'function') + { + promise.abort = response.abort; + } + return promise; }, /**