]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/rpc/RpcService.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / rpc / RpcService.js
1 if(!dojo._hasResource["dojo.rpc.RpcService"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.rpc.RpcService"] = true;
3 dojo.provide("dojo.rpc.RpcService");
4
5 dojo.declare("dojo.rpc.RpcService", null, {
6         constructor: function(args){
7                 //summary:
8                 //Take a string as a url to retrieve an smd or an object that is an smd or partial smd to use
9                 //as a definition for the service
10                 //
11                 //      args: object
12                 //              Takes a number of properties as kwArgs for defining the service.  It also
13                 //              accepts a string.  When passed a string, it is treated as a url from
14                 //              which it should synchronously retrieve an smd file.  Otherwise it is a kwArgs
15                 //              object.  It accepts serviceUrl, to manually define a url for the rpc service
16                 //              allowing the rpc system to be used without an smd definition. strictArgChecks
17                 //              forces the system to verify that the # of arguments provided in a call
18                 //              matches those defined in the smd.  smdString allows a developer to pass
19                 //              a jsonString directly, which will be converted into an object or alternatively
20                 //              smdObject is accepts an smdObject directly.
21                 //                              
22                 if(args){
23                         //if the arg is a string, we assume it is a url to retrieve an smd definition from
24                         if( (dojo.isString(args)) || (args instanceof dojo._Url)){
25                                 if (args instanceof dojo._Url){
26                                         var url = args + "";
27                                 }else{
28                                         url = args;
29                                 }
30                                 var def = dojo.xhrGet({
31                                         url: url,
32                                         handleAs: "json-comment-optional",
33                                         sync: true
34                                 });
35                                 
36                                 def.addCallback(this, "processSmd");
37                                 def.addErrback(function() {
38                                         throw new Error("Unable to load SMD from " + args);
39                                 });
40
41                         }else if(args.smdStr){
42                                 this.processSmd(dojo.eval("("+args.smdStr+")"));
43                         }else{
44                                 // otherwise we assume it's an arguments object with the following
45                                 // (optional) properties:
46                                 //      - serviceUrl
47                                 //      - strictArgChecks
48                                 //      - smdStr
49                                 //      - smdObj
50
51                                 if(args.serviceUrl){
52                                         this.serviceUrl = args.serviceUrl;
53                                 }
54
55                                 this.timeout = args.timeout || 3000;
56
57                                 if("strictArgChecks" in args){
58                                         this.strictArgChecks = args.strictArgChecks;
59                                 }
60
61                                 this.processSmd(args);
62                         }
63                 }
64         },
65
66         strictArgChecks: true,
67         serviceUrl: "",
68
69         parseResults: function(obj){
70                 // summary
71                 //              parse the results coming back from an rpc request.  this
72                 //              base implementation, just returns the full object
73                 //              subclasses should parse and only return the actual results
74                 //      obj: Object
75                 //              Object that is the return results from an rpc request
76                 return obj;
77         },
78
79         errorCallback: function(/* dojo.Deferred */ deferredRequestHandler){
80                 // summary:
81                 //              create callback that calls the Deferres errback method
82                 //      deferredRequestHandler: Deferred
83                 //              The deferred object handling a request.
84                 return function(data){
85                         deferredRequestHandler.errback(new Error(data.message));
86                 };
87         },
88
89         resultCallback: function(/* dojo.Deferred */ deferredRequestHandler){
90                 // summary:
91                 //              create callback that calls the Deferred's callback method
92                 //      deferredRequestHandler: Deferred
93                 //              The deferred object handling a request.
94
95                 var tf = dojo.hitch(this, 
96                         function(obj){
97                                 if(obj.error!=null){
98                                         var err;
99                                         if(typeof obj.error == 'object'){
100                                                 err = new Error(obj.error.message);
101                                                 err.code = obj.error.code;
102                                                 err.error = obj.error.error;
103                                         }else{
104                                                 err = new Error(obj.error);
105                                         }
106                                         err.id = obj.id;
107                                         err.errorObject = obj;
108                                         deferredRequestHandler.errback(err);
109                                 }else{
110                                         deferredRequestHandler.callback(this.parseResults(obj)); 
111                                 }
112                         }
113                 );
114                 return tf;
115         },
116
117         generateMethod: function(/*string*/ method, /*array*/ parameters, /*string*/ url){
118                 // summary:
119                 //              generate the local bind methods for the remote object
120                 //      method: string
121                 //              The name of the method we are generating
122                 //      parameters: array
123                 //              the array of parameters for this call.
124                 //      url: string
125                 //              the service url for this call
126
127                 return dojo.hitch(this, function(){
128                         var deferredRequestHandler = new dojo.Deferred();
129
130                         // if params weren't specified, then we can assume it's varargs
131                         if( (this.strictArgChecks) &&
132                                 (parameters != null) &&
133                                 (arguments.length != parameters.length)
134                         ){
135                                 // put error stuff here, no enough params
136                                 throw new Error("Invalid number of parameters for remote method.");
137                         }else{
138                                 this.bind(method, dojo._toArray(arguments), deferredRequestHandler, url);
139                         }
140
141                         return deferredRequestHandler;
142                 });
143         },
144
145         processSmd: function(object){
146                 // summary:
147                 //              callback method for reciept of a smd object.  Parse the smd
148                 //              and generate functions based on the description
149                 //      object:
150                 //              smd object defining this service.
151
152                 if(object.methods){
153                         dojo.forEach(object.methods, function(m){
154                                 if(m && m.name){
155                                         this[m.name] = this.generateMethod(     m.name,
156                                                                                 m.parameters, 
157                                                                                 m.url||m.serviceUrl||m.serviceURL);
158                                         if(!dojo.isFunction(this[m.name])){
159                                                 throw new Error("RpcService: Failed to create" + m.name + "()");
160                                                 /*console.debug("RpcService: Failed to create", m.name, "()");*/
161                                         }
162                                 }
163                         }, this);
164                 }
165
166                 this.serviceUrl = object.serviceUrl||object.serviceURL;
167                 this.required = object.required;
168                 this.smd = object;
169         }
170 });
171
172 }