Of caller, callee, stub and dispatcher
PAJAX is just such a framework that hides the complexity involved in calling a method on a remote object. In this example, we’re going to create a basic calculator in JavaScript where the operations are performed in a PHP class. Let’s start with the PHP class for our calculator:
[code lang="php"]
class Calculator extends PajaxRemote {
function add($x, $y) {
return $x + $y;
}
function multiply($x, $y) {
return $x * $y;
}
...
}
?>
[/code]
The class only needs to extend the base class PajaxRemote. This marker class is used to indicate that this class is remotable and thus, a stub can be created and an instance of this class can be created remotely.
In order to access this class within JavaScript, a stub class is required. It can be generated by including the following in the HTML file containing the JavaScript
[code lang="javascript"]
[/code]
The resulting stub class can then be used like a regular JavaScript object. The framework automatically takes care of marshaling the parameters and performs the remote call.
[code lang="javascript"]
[/code]
By default, the calls are performed synchronously. In most cases, it is not suitable to have the caller blocked while invoking methods on an object that lives across the network. This typically leads to a frozen UI and an overall unresponsive application.
Asynchronous Call
When you invoke a method asynchronously, the call returns immediately. Most usefull methods will return some value, so you need a way to retrieve the returned value asynchronously. This is done by specifying a listener companion object for each remote object. Each method in the remote object has a corresponding method on the listener, prefixed with “on”, that is automatically invoked upon completion of the remote call. Here is a diagram of the calling sequence:







