JSON Remote Procedure Calls
By now, you may have noticed that even after using Dojo's
various XHR methods such as dojo.xhrGet to reduce boilerplate, it is
still a somewhat redundant and error-prone operation to repeatedly
provide content to the call and write a load callback function. Fortunately, you can
use Dojo's RPC (Remote Procedure Call) machinery to mitigate some of
the monotony via Core's dojo.rpc
module. In short, you provide some configuration information via a
Simple Method Description (SMD), create an instance of this service by
passing in the configuration, and then use the service instead of the
xhrGet et al. If your application
has a fairly standard way of interacting with the server and responds
in very similar ways for error handling, etc., the benefit of using
the rpc module is that you'll
generally have a cleaner design that's less error-prone.
Currently, Core provides a JsonService and a JsonpService, which both descend from a base
class called RpcService.
Tip
The dojox.rpc module
provides additional RPC capabilities, some of which may soon be
migrated to Core.
JSON RPC Example
To illustrate some basic usage of the RPC machinery, let's
work through an example that uses JsonService to process a list of numbers,
providing the sum of the numbers or the sum of the sum of each
number squared. The client consists of an SMD that provides two
methods, sum and sumOfSquares, which both take a list of
numbers:
<html> <head> <title>Fun with JSON RPC!</title> <script type="text/javascript" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access