Code in this post can be obsolete, however, principles and theory may still apply.
<<< Mastering Ext.Direct, Part 2
Processing responses
As I've already said, you cannot use the return value of an API function call as result is not know at the moment of the call return. It is never stressed enough that
Ext.Direct calls are ASYNCHRONOUS.
Therefore, all Ext.Direct created API functions, for example Example.Car.go() take one extra argument in addition to arguments you have specified in server-side class definition. That argument is callback function.
In our example, Example.Car.go() takes one argument, speed per server side method definition, however, it takes two in fact: Example.Car.go(speed, callback)
Let's call it with a callback to test it. Type in the Firebug console:
console.info is a function that prints its arguments in a human readable form. We use it as the callback so we see:
- that it really gets called when the response arrives
- which arguments Ext.Direct passes to the callback
As you can see, the callback is called with two arguments:
response- it is that what is returned by the server-sideCar.go()method. It can be string (as in our example), boolean, object, array or void (nothing).e- it is Ext.Direct.Event object or, being more specific, Ext.Direct.RemotingEvent.
The event e has some useful properties and methods, most important of them are:
statusistrueif the call was successful,falseif the call failedresultcontains same value as the first argument - it is server response datatypeisrpcin our example but it also can beexceptionor othersactionis the class namemethodis name of the method calledgetTransaction()is method to retrieve the transaction used in this server round trip. You would use it if you want more data on the transaction.
Processing exceptions
Let's call go method again:
The callback is called as before but the arguments differ:
responseisundefined- no data from serverehas typeexceptionand has propertymessagethat contains error message text
An example application
That is almost all we need to know to start using Ext.Direct in an application. I've written a simple one so you have something to play with.
First, create file example.js with the following content:
Then tune your index.php so that it reads:
Conclusion
Good! Play with Ext.Direct and let me know what you would like to have in next parts.



12 comments
Comments are closed on archived posts.