egw.getCache(name) function to get a cache-object shared between all EGroupware windows and popups

used to share server-side generated options for et2_selectbox
also removed nextmatch specific code from et2_selectbox which seems no longer necessary
This commit is contained in:
Ralf Becker 2021-07-19 16:57:25 +02:00
parent cc27253b7b
commit 0a1e784f2e
2 changed files with 29 additions and 31 deletions

View File

@ -1469,29 +1469,11 @@ export class et2_selectbox extends et2_inputWidget
// normalize options by removing trailing commas // normalize options by removing trailing commas
options_string = options_string.replace(/,+$/, ''); options_string = options_string.replace(/,+$/, '');
var cache_id = widget._type+'_'+options_string; const cache_id = widget._type+'_'+options_string;
var cache_owner = ( const cache_owner = egw.getCache('et2_selectbox');
// Todo: @new-js-loader et2_selectbox is no longer instanciated globaly --> caching needs to be fixed let cache = cache_owner[cache_id];
et2_selectbox
/*egw.window.et2_selectbox ?
egw.window.et2_selectbox :
egw(window).window.et2_selectbox*/
).type_cache;
var cache = cache_owner[cache_id];
// Options for a selectbox in a nextmatch must be returned now, as the if (typeof cache === 'undefined')
// widget we have is not enough to set the options later.
var in_nextmatch = false;
if(typeof cache === 'undefined' || typeof cache.length === 'undefined')
{
var parent = widget._parent;
while(parent && !in_nextmatch)
{
in_nextmatch = parent && parent._type && parent._type === 'nextmatch';
parent = parent._parent;
}
}
if (typeof cache == 'undefined' || in_nextmatch)
{ {
// Fetch with json instead of jsonq because there may be more than // Fetch with json instead of jsonq because there may be more than
// one widget listening for the response by the time it gets back, // one widget listening for the response by the time it gets back,
@ -1499,7 +1481,7 @@ export class et2_selectbox extends et2_inputWidget
const req = egw.json( const req = egw.json(
'EGroupware\\Api\\Etemplate\\Widget\\Select::ajax_get_options', 'EGroupware\\Api\\Etemplate\\Widget\\Select::ajax_get_options',
[widget._type,options_string,attrs.value] [widget._type,options_string,attrs.value]
).sendRequest(); // was !in_nextmatch to send synchronous request ).sendRequest();
if(typeof cache === 'undefined') if(typeof cache === 'undefined')
{ {
cache_owner[cache_id] = req; cache_owner[cache_id] = req;
@ -1511,17 +1493,15 @@ export class et2_selectbox extends et2_inputWidget
// pending, wait for it // pending, wait for it
cache.then((response) => { cache.then((response) => {
cache = cache_owner[cache_id] = response.response[0].data||undefined; cache = cache_owner[cache_id] = response.response[0].data||undefined;
// Set select_options in attributes in case we get a resonse before // Set select_options in attributes in case we get a response before
// the widget is finished loading (otherwise it will re-set to {}) // the widget is finished loading (otherwise it will re-set to {})
attrs.select_options = cache; attrs.select_options = cache;
egw.window.setTimeout(() => { // Avoid errors if widget is destroyed before the timeout
// Avoid errors if widget is destroyed before the timeout if (widget && typeof widget.id !== 'undefined')
if (widget && typeof widget.id !== 'undefined') {
{ widget.set_select_options(et2_selectbox.find_select_options(widget,{}, widget.options));
widget.set_select_options(et2_selectbox.find_select_options(widget,{}, widget.options)); }
}
}, 1);
}); });
return []; return [];
} }

View File

@ -145,9 +145,27 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
var uid_counter = 0; var uid_counter = 0;
/**
* Global cache shared between all EGroupware windows
* @type {{}}
*/
const cache = {};
// Create the utils object which contains references to all functions // Create the utils object which contains references to all functions
// covered by it. // covered by it.
var utils = { var utils = {
/**
* Get a cache object shared between all EGroupware windows
*
* @param {string} _name unique name for the cache-object
* @return {*}
*/
getCache: function(_name)
{
if (typeof cache[_name] === 'undefined') cache[_name] = {};
return cache[_name];
},
ajaxUrl: function(_menuaction) { ajaxUrl: function(_menuaction) {
if(_menuaction.indexOf('menuaction=') >= 0) if(_menuaction.indexOf('menuaction=') >= 0)