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){
9 return declare("dojo.rpc.RpcService", null, {
13 constructor: function(args){
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
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.
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){
39 handleAs: "json-comment-optional",
43 def.addCallback(this, "processSmd");
44 def.addErrback(function(){
45 throw new Error("Unable to load SMD from " + args);
48 }else if(args.smdStr){
49 this.processSmd(kernel.eval("("+args.smdStr+")"));
51 // otherwise we assume it's an arguments object with the following
52 // (optional) properties:
59 this.serviceUrl = args.serviceUrl;
62 this.timeout = args.timeout || 3000;
64 if("strictArgChecks" in args){
65 this.strictArgChecks = args.strictArgChecks;
68 this.processSmd(args);
73 strictArgChecks: true,
76 parseResults: function(obj){
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
82 // Object that is the return results from an rpc request
86 errorCallback: function(/* dojo/_base/Deferred */ deferredRequestHandler){
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);
96 resultCallback: function(/* dojo/_base/Deferred */ deferredRequestHandler){
98 // create callback that calls the Deferred's callback method
99 // deferredRequestHandler: Deferred
100 // The deferred object handling a request.
102 return lang.hitch(this,
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;
111 err = new Error(obj.error);
114 err.errorObject = obj;
115 deferredRequestHandler.errback(err);
117 deferredRequestHandler.callback(this.parseResults(obj));
123 generateMethod: function(/*string*/ method, /*array*/ parameters, /*string*/ url){
125 // generate the local bind methods for the remote object
127 // The name of the method we are generating
129 // the array of parameters for this call.
131 // the service url for this call
133 return lang.hitch(this, function(){
134 var deferredRequestHandler = new Deferred();
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)
141 // put error stuff here, no enough params
142 throw new Error("Invalid number of parameters for remote method.");
144 this.bind(method, lang._toArray(arguments), deferredRequestHandler, url);
147 return deferredRequestHandler;
151 processSmd: function(object){
153 // callback method for receipt of a smd object. Parse the smd
154 // and generate functions based on the description
156 // smd object defining this service.
159 array.forEach(object.methods, function(m){
161 this[m.name] = this.generateMethod( m.name,
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, "()");*/
172 this.serviceUrl = object.serviceUrl||object.serviceURL;
173 this.required = object.required;