Xajax 0.5: Tips and Tricks: Call registered function from registered function
Xajax 0.5: Tips and Tricks: Call registered function from registered function
Contents |
[edit] Using the xajax response to invoke another xajax enabled function
[edit] Using the script command
This method is quite easy to use, but less flexible than the next method. The script command can be used to invoke nearly any javascript on the browser. However, the script command should not be over-used as it is not always as efficient as using other built-in response commands. If a piece of script will be used repeatedly, it is best to build a plugin. See: Custom Response Plugins
function minorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in minor function...');
return $objResponse;
}
function majorFunction() {
$objResponse = new xajaxResponse();
$objResponse->script('xajax_minorFunction();');
return $objResponse;
}
[edit] Using the call command (passing parameters)
This method is more flexible as you can easily pass parameters and xajax will encode and decode the parameter values between the server and the browser.
function minorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in minor function...');
return $objResponse;
}
function majorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in major function...');
$objResponse->call('xajax_minorFunction');
return $objResponse;
}
or with parameter(s):
function majorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in major function...');
$parameter = "myValue";
$objResponse->call('xajax_minorFunction', $parameter);
return $objResponse;
}
[edit] Keeping it on the server
This avoids the slow loop you get from sending a command to the client to have it request data from the server again.
[edit] Passing by reference
This method is the least desirable of the three server side solutions. However, this method provides an example of how you could design a library of functions which do not need to be invoked separately. Since the xajax response object is passed as a parameter, it makes the function (nearly) impossible to call directly from the browser side. However, the xajax response is instatiated once and the resulting commands do not need to be copied from one response object to another as you'll see in the next method.
function minorFunction(&$objResponse) {
$objResponse->alert('in minor function');
return $objResponse;
}
function majorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in major function');
return $objResponse;
}
[edit] Using loadCommands
This method is quite popular and seems to be the middle ground between ease of use and performance. The xajax response object is instatiated once per function call and the response commands have to be copied from one response object to the main response object. However, the response object does not have to be passed as a parameter, making it easier to work with. With large responses, this method could effect server performance as the response objects are instatiated and the commands copied (in the loadCommands function).
function minorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in minor function');
return $objResponse;
}
function majorFunction() {
$objResponse = new xajaxResponse();
$objResponse->alert('in major function');
$objResponse->loadCommands(minorFunction());
return $objResponse;
}
[edit] Using a global response
This method seems to be the least wasteful with regards to performance and memory usage as the xajax object is only instatiated once, regardless of the number of functions that are called. The response commands are added directly to the global response object, so the loadCommands function is not necessary.
$objResponse = new xajaxResponse();
function minorFunction() {
global $objResponse;
$objResponse->alert('in minor function');
return $objResponse;
}
function majorFunction() {
global $objResponse;
$objResponse->alert('in major function');
return $objResponse;
}
- Author: CtC - Joseph Woolley - 2007/03/04
- Contributions from: BigBrownChunx - 2007/03/04