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");
5 dojo.declare("dojo.rpc.RpcService", null, {
6 constructor: function(args){
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
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.
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){
30 var def = dojo.xhrGet({
32 handleAs: "json-comment-optional",
36 def.addCallback(this, "processSmd");
37 def.addErrback(function() {
38 throw new Error("Unable to load SMD from " + args);
41 }else if(args.smdStr){
42 this.processSmd(dojo.eval("("+args.smdStr+")"));
44 // otherwise we assume it's an arguments object with the following
45 // (optional) properties:
52 this.serviceUrl = args.serviceUrl;
55 this.timeout = args.timeout || 3000;
57 if("strictArgChecks" in args){
58 this.strictArgChecks = args.strictArgChecks;
61 this.processSmd(args);
66 strictArgChecks: true,
69 parseResults: function(obj){
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
75 // Object that is the return results from an rpc request
79 errorCallback: function(/* dojo.Deferred */ deferredRequestHandler){
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));
89 resultCallback: function(/* dojo.Deferred */ deferredRequestHandler){
91 // create callback that calls the Deferred's callback method
92 // deferredRequestHandler: Deferred
93 // The deferred object handling a request.
95 var tf = dojo.hitch(this,
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;
104 err = new Error(obj.error);
107 err.errorObject = obj;
108 deferredRequestHandler.errback(err);
110 deferredRequestHandler.callback(this.parseResults(obj));
117 generateMethod: function(/*string*/ method, /*array*/ parameters, /*string*/ url){
119 // generate the local bind methods for the remote object
121 // The name of the method we are generating
123 // the array of parameters for this call.
125 // the service url for this call
127 return dojo.hitch(this, function(){
128 var deferredRequestHandler = new dojo.Deferred();
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)
135 // put error stuff here, no enough params
136 throw new Error("Invalid number of parameters for remote method.");
138 this.bind(method, dojo._toArray(arguments), deferredRequestHandler, url);
141 return deferredRequestHandler;
145 processSmd: function(object){
147 // callback method for reciept of a smd object. Parse the smd
148 // and generate functions based on the description
150 // smd object defining this service.
153 dojo.forEach(object.methods, function(m){
155 this[m.name] = this.generateMethod( m.name,
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, "()");*/
166 this.serviceUrl = object.serviceUrl||object.serviceURL;
167 this.required = object.required;