Add support for global json handlers, and register a global handler for storing data entries

This commit is contained in:
Nathan Gray 2016-02-04 19:26:37 +00:00
parent d2a416baae
commit 62eb8e00f2
2 changed files with 75 additions and 27 deletions

View File

@ -519,6 +519,25 @@ egw.extend("data_storage", egw.MODULE_GLOBAL, function (_app, _wnd) {
*/ */
var registeredCallbacks = {}; var registeredCallbacks = {};
/**
* Register the "data" plugin globally for single uids
* Multiple UIDs such as nextmatch results are still handled by egw.data
* using dataFetch() && parseServerResponse(), above. Both update the
* GLOBAL data cache though this one is registered globally, and the above
* is registered app local.
*/
egw.registerJSONPlugin(function(type, res, req) {
if ((typeof res.data.uid != 'undefined') &&
(typeof res.data.data != 'undefined'))
{
// Store it, which will call all registered listeners
this.dataStoreUID(res.data.uid, res.data.data);
return true;
}
}, egw, 'data',true);
/** /**
* Uids and timers used for querying data uids, hashed by the first few * Uids and timers used for querying data uids, hashed by the first few
* bytes of the _execId, stored as an object of the form * bytes of the _execId, stored as an object of the form

View File

@ -31,6 +31,15 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) {
*/ */
var plugins = {}; var plugins = {};
/**
* Global json handlers are from global modules, not window level
*/
if(typeof egw._global_json_handlers == 'undefined')
{
egw._global_json_handlers = {}
}
var global_plugins = egw._global_json_handlers;
/** /**
* Internal implementation of the JSON request object. * Internal implementation of the JSON request object.
* *
@ -165,24 +174,29 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) {
if(typeof res.type == 'string' && res.type != 'data') only_data = false; if(typeof res.type == 'string' && res.type != 'data') only_data = false;
// Check whether a plugin for the given type exists // Check whether a plugin for the given type exists
if (typeof plugins[res.type] !== 'undefined') var handlers = [plugins, global_plugins];
for(var handler_idx = 0; handler_idx < handlers.length; handler_idx++)
{ {
for (var j = 0; j < plugins[res.type].length; j++) { var handler_level = handlers[handler_idx];
try { if (typeof handler_level[res.type] !== 'undefined')
// Get a reference to the plugin {
var plugin = plugins[res.type][j]; for (var j = 0; j < handler_level[res.type].length; j++) {
try {
// Get a reference to the plugin
var plugin = handler_level[res.type][j];
// Call the plugin callback // Call the plugin callback
plugin.callback.call( plugin.callback.call(
plugin.context ? plugin.context : this.context, plugin.context ? plugin.context : this.context,
res.type, res, this res.type, res, this
); );
} catch(e) { } catch(e) {
var msg = e.message ? e.message : e + ''; var msg = e.message ? e.message : e + '';
var stack = e.stack ? "\n-- Stack trace --\n" + e.stack : ""; var stack = e.stack ? "\n-- Stack trace --\n" + e.stack : "";
this.egw.debug('error', 'Exception "' + msg + '" while handling JSON response from ' + this.egw.debug('error', 'Exception "' + msg + '" while handling JSON response from ' +
this.url + ' [' + JSON.stringify(this.parameters) + '] type "' + res.type + this.url + ' [' + JSON.stringify(this.parameters) + '] type "' + res.type +
'", plugin', plugin, 'response', res, stack); '", plugin', plugin, 'response', res, stack);
}
} }
} }
} }
@ -229,23 +243,32 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) {
* @param _type is an optional parameter defaulting to 'global'. * @param _type is an optional parameter defaulting to 'global'.
* it describes the response type which this plugin should be * it describes the response type which this plugin should be
* handling. * handling.
* @param {boolean} [_global=false] Register the handler globally or
* locally. Global handlers must stay around, so should be used
* for global modules.
*/ */
registerJSONPlugin: function(_callback, _context, _type) registerJSONPlugin: function(_callback, _context, _type, _global)
{ {
// _type defaults to 'global' // _type defaults to 'global'
if (typeof _type === 'undefined') if (typeof _type === 'undefined')
{ {
_type = 'global'; _type = 'global';
} }
// _global defaults to false
if (typeof _global === 'undefined')
{
_global = false;
}
var scoped = _global ? global_plugins : plugins;
// Create an array for the given category inside the plugins object // Create an array for the given category inside the plugins object
if (typeof plugins[_type] === 'undefined') if (typeof scoped[_type] === 'undefined')
{ {
plugins[_type] = []; scoped[_type] = [];
} }
// Add the entry // Add the entry
plugins[_type].push({ scoped[_type].push({
'callback': _callback, 'callback': _callback,
'context': _context 'context': _context
}); });
@ -261,22 +284,28 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) {
* @param _type is an optional parameter defaulting to 'global'. * @param _type is an optional parameter defaulting to 'global'.
* it describes the response type which this plugin should be * it describes the response type which this plugin should be
* handling. * handling.
* @param {boolean} [_global=false] Remove a global or local handler.
*/ */
unregisterJSONPlugin: function(_callback, _context, _type) unregisterJSONPlugin: function(_callback, _context, _type, _global)
{ {
// _type defaults to 'global' // _type defaults to 'global'
if (typeof _type === 'undefined') if (typeof _type === 'undefined')
{ {
_type = 'global'; _type = 'global';
} }
// _global defaults to false
if (typeof plugins[_type] !== 'undefined') { if (typeof _global === 'undefined')
for (var i = 0; i < plugins[_type].length; i++) {
_global = false;
}
var scoped = _global ? global_plugins : plugins;
if (typeof scoped[_type] !== 'undefined') {
for (var i = 0; i < scoped[_type].length; i++)
{ {
if (plugins[_type][i].callback == _callback && if (scoped[_type][i].callback == _callback &&
plugins[_type][i].context == _context) scoped[_type][i].context == _context)
{ {
plugins[_type].slice(i, 1); scoped[_type].slice(i, 1);
break; break;
} }
} }