From c3c887da9828c94b28b890a9fe4bbec206d191de Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Tue, 9 Oct 2018 17:41:27 +0200 Subject: [PATCH] Fix set_account_data turns into an infinite loop because of async ajax call --- api/js/jsapi/egw_user.js | 41 +++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/api/js/jsapi/egw_user.js b/api/js/jsapi/egw_user.js index fe2bfb87d8..42264b113c 100644 --- a/api/js/jsapi/egw_user.js +++ b/api/js/jsapi/egw_user.js @@ -183,7 +183,7 @@ egw.extend('user', egw.MODULE_GLOBAL, function() } _callback.call(_context, data); } - ).sendRequest(); + ).sendRequest(); } else { @@ -214,20 +214,47 @@ egw.extend('user', egw.MODULE_GLOBAL, function() if (field.indexOf('{') == -1) { data = _data[user]; + target.set_value(data); } else { data = field; - var match; - while((match = data.match(/{([^}]+)}/))) + + /** + * resolve given data whilst the condition met + */ + const resolveData = function(_d, condition, action) { + var whilst = function (_d) { + return condition(_d) ? action(condition(_d)).then(whilst) : Promise.resolve(_d); + } + return whilst(_d); + }; + + /** + * get data promise + */ + const getData = function(_match) { - egw.accountData(user, match[1], false, function(_data) + var match = _match; + return new Promise(function(resolve) { - data = data.replace(/{([^}]+)}/, _data[user]); + egw.accountData(user, match, false, function(_d) + { + data = data.replace(/{([^}]+)}/, _d[user]); + resolve(data); + }); }); - } + }; + + // run rsolve data + resolveData(data, function(_d){ + var r = _d.match(/{([^}]+)}/); + return r && r.length > 0 ? r[1] : r; + }, + getData).then(function(data){ + target.set_value(data) + }); } - target.set_value(data); }); }; },