Added 'jquery' method to the json handler which allows it to call functions of jquery object from the server

This commit is contained in:
Andreas Stöckel 2010-06-10 12:24:50 +00:00
parent bd023081e6
commit ad1c1f1f06
2 changed files with 37 additions and 34 deletions

View File

@ -279,14 +279,14 @@ class egw_json_response
* *
* @param string $script the script code which should be executed upon receiving * @param string $script the script code which should be executed upon receiving
*/ */
public function each($selector,$method,array $parameters=array()) public function jquery($selector,$method,array $parameters=array())
{ {
if (is_string($selector) && is_string($method)) if (is_string($selector) && is_string($method))
{ {
$this->addGeneric('each', array( $this->addGeneric('jquery', array(
'select' => $selector, 'select' => $selector,
'func' => $method, 'func' => $method,
'params' => $parameters, 'parms' => $parameters,
)); ));
} }
else else

View File

@ -143,6 +143,14 @@ egw_json_request.prototype.alertFunc = function(_message, _details)
alert(_message); alert(_message);
} }
function _egw_json_debug_log(_msg)
{
if (typeof console != "undefined" && typeof console.log != "undefined")
{
console.log(_msg);
}
}
/* Internal function which handles the response from the server */ /* Internal function which handles the response from the server */
egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest) egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest)
{ {
@ -151,29 +159,31 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
var hasResponse = false; var hasResponse = false;
for (var i = 0; i < data.response.length; i++) for (var i = 0; i < data.response.length; i++)
{ {
var res = data.response[i];
switch (data.response[i].type) switch (data.response[i].type)
{ {
case 'alert': case 'alert':
//Check whether all needed parameters have been passed and call the alertHandler function //Check whether all needed parameters have been passed and call the alertHandler function
if ((typeof data.response[i].data.message != 'undefined') && if ((typeof res.data.message != 'undefined') &&
(typeof data.response[i].data.details != 'undefined')) (typeof res.data.details != 'undefined'))
{ {
this.alertHandler( this.alertHandler(
data.response[i].data.message, res.data.message,
data.response[i].data.details) res.data.details)
hasResponse = true; hasResponse = true;
} }
break; break;
case 'assign': case 'assign':
//Check whether all needed parameters have been passed and call the alertHandler function //Check whether all needed parameters have been passed and call the alertHandler function
if ((typeof data.response[i].data.id != 'undefined') && if ((typeof res.data.id != 'undefined') &&
(typeof data.response[i].data.key != 'undefined') && (typeof res.data.key != 'undefined') &&
(typeof data.response[i].data.value != 'undefined')) (typeof res.data.value != 'undefined'))
{ {
var obj = document.getElementById(data.response[i].data.id); var obj = document.getElementById(res.data.id);
if (obj) if (obj)
{ {
obj[data.response[i].data.key] = data.response[i].data.value; obj[res.data.key] = res.data.value;
hasResponse = true; hasResponse = true;
} }
} }
@ -182,53 +192,46 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
//Callback the caller in order to allow him to handle the data //Callback the caller in order to allow him to handle the data
if (this.callback) if (this.callback)
{ {
this.callback.call(this.sender, data.response[i].data); this.callback.call(this.sender, res.data);
hasResponse = true; hasResponse = true;
} }
break; break;
case 'script': case 'script':
if (typeof data.response[i].data == 'string') if (typeof res.data == 'string')
{ {
try try
{ {
var func = function() {eval(data.response[i].data);}; var func = function() {eval(res.data);};
func.call(window); func.call(window);
} }
catch (e) catch (e)
{ {
if (typeof console != "undefined" && typeof console.log != "undefined") e.code = res.data;
{ _egw_json_debug_log(e);
e.code = data.response[i].data;
console.log(e);
}
} }
hasResponse = true; hasResponse = true;
} }
break; break;
case 'each': case 'jquery':
if (typeof data.response[i].select == 'string' && typeof data.response[i].func == 'string') if (typeof res.data.select == 'string' &&
typeof res.data.func == 'string')
{ {
try try
{ {
var func = data.response[i].func; var jQueryObject = $(res.data.select);
// todo: for N>2 jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
$(data.response[i].select).each(func.call(data.response[i].parms[0],data.response[i].parms[1],data.response[i].parms[2]));
} }
catch (e) catch (e)
{ {
if (typeof console != "undefined" && typeof console.log != "undefined") _egw_json_debug_log(e);
{
e.code = data.response[i];
console.log(e);
}
} }
hasResponse = true; hasResponse = true;
} }
break; break;
case 'redirect': case 'redirect':
if (typeof data.response[i].data == 'string') if (typeof res.data == 'string')
{ {
window.location.href = data.response[i].data; window.location.href = res.data;
} }
break; break;
} }
@ -313,7 +316,7 @@ function _egw_json_getFormValues(serialized, children)
function _egw_json_getFormValue(serialized, child) function _egw_json_getFormValue(serialized, child)
{ {
//Return if the child doesn't have a name, is disabled, or is a radio-/checkbox and not checked //Return if the child doesn't have a name, is disabled, or is a radio-/checkbox and not checked
if ((!child.name) || (child.disabled && child.disabled == true) || if ((typeof child.name == "undefined") || (child.disabled && child.disabled == true) ||
(child.type && (child.type == 'radio' || child.type == 'checkbox') && (!child.checked))) (child.type && (child.type == 'radio' || child.type == 'checkbox') && (!child.checked)))
{ {
return; return;