source: Dev/branches/rest-dojo-ui/client/dojox/rpc/JsonRPC.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 1.2 KB
Line 
1define("dojox/rpc/JsonRPC", ["dojo", "dojox", "dojox/rpc/Service"], function(dojo, dojox) {
2
3        function jsonRpcEnvelope(version){
4                return {
5                        serialize: function(smd, method, data, options){
6                                //not converted to json it self. This  will be done, if
7                                //appropriate, at the transport level
8       
9                                var d = {
10                                        id: this._requestId++,
11                                        method: method.name,
12                                        params: data
13                                };
14                                if(version){
15                                        d.jsonrpc = version;
16                                }
17                                return {
18                                        data: dojo.toJson(d),
19                                        handleAs:'json',
20                                        contentType: 'application/json',
21                                        transport:"POST"
22                                };
23                        },
24       
25                        deserialize: function(obj){
26                                if ('Error' == obj.name){
27                                        obj = dojo.fromJson(obj.responseText);
28                                }
29                                if(obj.error) {
30                                        var e = new Error(obj.error.message || obj.error);
31                                        e._rpcErrorObject = obj.error;
32                                        return e;
33                                }
34                                return obj.result;
35                        }
36                };
37        }
38        dojox.rpc.envelopeRegistry.register(
39                "JSON-RPC-1.0",
40                function(str){
41                        return str == "JSON-RPC-1.0";
42                },
43                dojo.mixin({namedParams:false},jsonRpcEnvelope()) // 1.0 will only work with ordered params
44        );
45
46        dojox.rpc.envelopeRegistry.register(
47                "JSON-RPC-2.0",
48                function(str){
49                        return str == "JSON-RPC-2.0";
50                },
51                jsonRpcEnvelope("2.0")
52        );
53
54});
Note: See TracBrowser for help on using the repository browser.