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