source: Dev/branches/rest-dojo-ui/client/dojo/rpc/JsonService.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: 2.3 KB
Line 
1define(["../main", "./RpcService"], function(dojo) {
2        // module:
3        //              dojo/rpc/JsonService
4        // summary:
5        //              TODOC
6
7
8dojo.declare("dojo.rpc.JsonService", dojo.rpc.RpcService, {
9                bustCache: false,
10                contentType: "application/json-rpc",
11                lastSubmissionId: 0,
12
13                callRemote: function(method, params){
14                        // summary:
15                        //              call an arbitrary remote method without requiring it to be
16                        //              predefined with SMD
17                        //      method: string
18                        //              the name of the remote method you want to call.
19                        //      params: array
20                        //              array of parameters to pass to method
21
22                        var deferred = new dojo.Deferred();
23                        this.bind(method, params, deferred);
24                        return deferred;
25                },
26
27                bind: function(method, parameters, deferredRequestHandler, url){
28                        //summary:
29                        //              JSON-RPC bind method. Takes remote method, parameters,
30                        //              deferred, and a url, calls createRequest to make a JSON-RPC
31                        //              envelope and passes that off with bind.
32                        //      method: string
33                        //              The name of the method we are calling
34                        //      parameters: array
35                        //              The parameters we are passing off to the method
36                        //      deferredRequestHandler: deferred
37                        //              The Deferred object for this particular request
38
39                        var def = dojo.rawXhrPost({
40                                url: url||this.serviceUrl,
41                                postData: this.createRequest(method, parameters),
42                                contentType: this.contentType,
43                                timeout: this.timeout,
44                                handleAs: "json-comment-optional"
45                        });
46                        def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler));
47                },
48
49                createRequest: function(method, params){
50                        // summary:
51                        //      create a JSON-RPC envelope for the request
52                        //      method: string
53                        //              The name of the method we are creating the requst for
54                        //      params: array
55                        //              The array of parameters for this request;
56
57                        var req = { "params": params, "method": method, "id": ++this.lastSubmissionId };
58                        return dojo.toJson(req);
59                },
60
61                parseResults: function(/*anything*/obj){
62                        //summary:
63                        //              parse the result envelope and pass the results back to
64                        //              the callback function
65                        //      obj: Object
66                        //              Object containing envelope of data we recieve from the server
67
68                        if(dojo.isObject(obj)){
69                                if("result" in obj){
70                                        return obj.result;
71                                }
72                                if("Result" in obj){
73                                        return obj.Result;
74                                }
75                                if("ResultSet" in obj){
76                                        return obj.ResultSet;
77                                }
78                        }
79                        return obj;
80                }
81        }
82);
83
84return dojo.rpc.JsonService;
85});
Note: See TracBrowser for help on using the repository browser.