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