Using PHPObject (Sequential Invoking)
The delayExecute() and execute() methods
If you have more than one task to perform on the server, without flash having to handle the data until the last task is completed, you should use the delayExecute() and execute() methods of PHPObject to invoke remote methods in batches.
Before we proceed, we look at what goes behind the scenes. When a remote method is called, PHPObject serializes the properties of the object making the call, and transmits this data to the php class. After the remote method completes, php returns data back to flash. If you call remote methods one after another the usual way, you will be wasting bandwidth since you are transmitting the same object properties to and fro the server. You will also be wasting CPU resources serializing and unserializing the same data. Since the php class already knows the latest object properties, it should proceed to perform the next tasks before returning results back to flash.
Here is an example of how we would invoke remote methods sequentially in PHPObject:
////////////////////////////////
// Creates the PHPObject
////////////////////////////////
myFoo = new PHPObject("Foo");
////////////////////////////////
// Set up responders as usual
////////////////////////////////
myFoo.doRemoteMethod3_onResult = function(result) {
trace(result);
}
////////////////////////////////
// Call the delayExecute() method
////////////////////////////////
myFoo.delayExecute();
////////////////////////////////
// Call the remote methods in the
// order you want them executed
////////////////////////////////
myFoo.doRemoteMethod1(param1,param2,...);
myFoo.doRemoteMethod2(param1,param2,...);
myFoo.doRemoteMethod1(param1,param2,...);
myFoo.doRemoteMethod3(param1,param2,...);
////////////////////////////////
// Invoke the remote methods
////////////////////////////////
myFoo.execute();
In the above example, data is transmitted to and fro the server only once. The php class will first execute doRemoteMethod1(), then doRemoteMethod2(), then doRemoteMethod1() again, and then doRemoteMethod3(), before sending the final data back to flash. Since the last method called is doRemoteMethod3, this method's responder will be triggered when the data returns.
Queuing remote calls
Using this same feature of PHPObject, we can queue remote calls according to users' interactions. Here is an example:
////////////////////////////////
// Creates the PHPObject
////////////////////////////////
myFoo = new PHPObject("Foo");
////////////////////////////////
// Responder
////////////////////////////////
myFoo.onResult = function(result) {
this.execute();
trace(result);
}
////////////////////////////////
// The buttons
////////////////////////////////
query1_btn.onRelease = function() {
myFoo.getResults(query1_txt.text);
myFoo.delayExecute();
}
query2_btn.onRelease = function() {
myFoo.getResults(query2_txt.text);
myFoo.delayExecute();
}
In the above example, the first time a button is clicked, the remote method is executed, and subsequent clicks only cause the methods to be stored. Then onResult, the stored methods are pushed to the server (and the returned result from the previous execution processed). And the cycle starts all over...
>> Documentation <<
|
|