dojo.provide("dojox.rpc.Service"); dojo.require("dojo.AdapterRegistry"); dojo.declare("dojox.rpc.Service", null, { constructor: function(smd, options){ // summary: // Take a string as a url to retrieve an smd or an object that is an smd or partial smd to use // as a definition for the service // // smd: object // Takes a number of properties as kwArgs for defining the service. It also // accepts a string. When passed a string, it is treated as a url from // which it should synchronously retrieve an smd file. Otherwise it is a kwArgs // object. It accepts serviceUrl, to manually define a url for the rpc service // allowing the rpc system to be used without an smd definition. strictArgChecks // forces the system to verify that the # of arguments provided in a call // matches those defined in the smd. smdString allows a developer to pass // a jsonString directly, which will be converted into an object or alternatively // smdObject is accepts an smdObject directly. // // description: // dojox.rpc.Service must be loaded prior to any plugin services like dojox.rpc.Rest // dojox.rpc.JsonRpc in order for them to register themselves, otherwise you get // a "No match found" error. var url; var self = this; function processSmd(smd){ smd._baseUrl = new dojo._Url(location.href,url || '.') + ''; self._smd = smd; //generate the methods for(var serviceName in self._smd.services){ var pieces = serviceName.split("."); // handle "namespaced" services by breaking apart by . var current = self; for(var i=0; i< pieces.length-1; i++){ // create or reuse each object as we go down the chain current = current[pieces[i]] || (current[pieces[i]] = {}); } current[pieces[pieces.length-1]]= self._generateService(serviceName, self._smd.services[serviceName]); } } if(smd){ //ifthe arg is a string, we assume it is a url to retrieve an smd definition from if( (dojo.isString(smd)) || (smd instanceof dojo._Url)){ if(smd instanceof dojo._Url){ url = smd + ""; }else{ url = smd; } var text = dojo._getText(url); if(!text){ throw new Error("Unable to load SMD from " + smd); }else{ processSmd(dojo.fromJson(text)); } }else{ processSmd(smd); } } this._options = (options ? options : {}); this._requestId = 0; }, _generateService: function(serviceName, method){ if(this[method]){ throw new Error("WARNING: "+ serviceName+ " already exists for service. Unable to generate function"); } method.name = serviceName; var func = dojo.hitch(this, "_executeMethod",method); var transport = dojox.rpc.transportRegistry.match(method.transport || this._smd.transport); if(transport.getExecutor){ func = transport.getExecutor(func,method,this); } var schema = method.returns || (method._schema = {}); // define the schema var servicePath = '/' + serviceName +'/'; // schemas are minimally used to track the id prefixes for the different services schema._service = func; func.servicePath = servicePath; func._schema = schema; func.id = dojox.rpc.Service._nextId++; return func; }, _getRequest: function(method,args){ var smd = this._smd; var envDef = dojox.rpc.envelopeRegistry.match(method.envelope || smd.envelope || "NONE"); if(envDef.namedParams){ // the serializer is expecting named params if((args.length==1) && dojo.isObject(args[0])){ // looks like we have what we want args = args[0]; }else{ // they provided ordered, must convert var data={}; for(var i=0;i