Tutorial: Call registered function from registered function

DOCS & TUTORIALS » Call registered function from registered function

Using the xajax response to invoke another xajax enabled function

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

Code: php

 
function minorFunction() {
    $objResponse = new xajaxResponse();
    $objResponse->alert('in minor function...');
    return $objResponse;
}
 
function majorFunction() {
    $objResponse = new xajaxResponse();
    $objResponse->script('xajax_minorFunction();');
    return $objResponse;
}
 



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.

Code: php

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):

Code: php

 
function majorFunction() {
    $objResponse = new xajaxResponse();
    $objResponse->alert('in major function...');
    $parameter = "myValue";
    $objResponse->call('xajax_minorFunction', $parameter);
    return $objResponse;
}
 



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.
 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.

Code: php

function minorFunction(&$objResponse) {
    $objResponse->alert('in minor function');
    return $objResponse;
}
 
function majorFunction() {
    $objResponse = new xajaxResponse();
    $objResponse->alert('in major function');
    return $objResponse;
}



 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).

Code: php

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;
}



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.

Code: php

 
$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;
}
 




 
DOCS & TUTORIALS » Call registered function from registered function


XAJAX on SourceForge.net
Support this project Valid XHTML 1.0 Transitional