- The Book of Dojo
- The Dojo Book, 0.4
- Part 1: "Introduction"
- Part 2: "Out of the Box" Dojo
- Part 3: "The Dojo Programming Model"
- Part 4: "More on Widgets"
- Part 5: "Connecting the pieces"
- Part 6: "Customizing Dojo Builds for Better Performance"
- Part 7: "Utilities"
- Part 8: "Internationalization and Accessiblity"
- Part 9: "Dojo Community"
- Part 10: "Fresh From The Shed" Dojo
- BookWriting
- Glossary
Server communication
Submitted by ilia on Wed, 08/23/2006 - 03:33.
To talk with server, one should use TreeLoadingControllerV3 or TreeRpcControllerV3. They inherit from TreeBasicControllerV3 and override its methods to deliver remote calls possibility.
TreeLoadingControllerV3 contains main methods for server calls, and allows dynamic node loading. TreeRpcControllerV3 adds server calls to tree manupulations like "createChild/move/edit...".
Url settings
All requests go through dojo.io.bind, usually via XMLHttpRequest transport. contains basic Url for all requests, e.g "http://site.com/remoteTreeService.do". You can have query string in it also.
every call adds special action parameter to query string to distinguish between call types. Actions are move, createChild..
For children loading, the action is getChildren.
An example url for such action would be "http://site.com/remoteTreeService.do?action=getChildren".
Most actions imply additional data parameter with information about node/tree and other action details server may want to know.
This way of composing an url is described in getRpcUrl, feel free to override if need.
Request format
data parameter is JSON-serialized. It usually sends some information about involved nodes and position. If you want to extend it somehow,- override method of controller that corresponds your action, your changes will affect this action only,
- override getInfo method of node/tree to affect parameters globally
- override getInfo method of controller if that's the right place =)
Response format
All data is JSON-serialized. There are libraries for JSON in most of programming languages.Server response is evaluated as javascript. That means you can embed any javascript, that will be evaluated on client-side. But it should return object. Use object error property to signalize about server-side error.
Good answer:
dojo.debug('I can also put javascript in server answer');
([{title:"test",isFolder:true,objectId:"myobj"},{title:"test2",children:[ {title:"test2.1"} ]}])
Good answer:
({})
Good answer, will return dojo.RpcError
({error: "Permission denied"})
Bad answer format (string), will return dojo.FormatError
Exception: blabla at line 50
Transport error (e.g 404) will also return dojo.CommunicationError
If you don't know what to return, return ({}). That means just "ok". Note outer brackets, they are needed to make sure it evaluates to javascript Object.
Callbacks and Error handling
Any request may be performed in synchroneous and asynchronous manner. Both of them return dojo.Deferred object, but for synchroneous call, it will be called until next script line.
You can call deferred.addCallback / deferred.addErrback to add your actions.
An example of usage would be
var deferred = loadingController.expandAll(tree);
// add action when operation finishes successfully
deferred.addCallback(function() { alert('expanded all nodes!'); });
// process error
deferred.addErrback(function(err) { dojo.debugShallow(err); });
More information about Deferred class and asynchronous programming can be found at http://mochikit.com/doc/html/MochiKit/Async.html (dojo implementation is Mochikit port), http://twistedmatrix.com/projects/core/documentation/howto/async.html (python implementation and a nice state-of-art intro).
