From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dojox/rpc/Service.js | 293 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 includes/js/dojox/rpc/Service.js (limited to 'includes/js/dojox/rpc/Service.js') diff --git a/includes/js/dojox/rpc/Service.js b/includes/js/dojox/rpc/Service.js new file mode 100644 index 0000000..438ee6f --- /dev/null +++ b/includes/js/dojox/rpc/Service.js @@ -0,0 +1,293 @@ +if(!dojo._hasResource["dojox.rpc.Service"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.rpc.Service"] = true; +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. + // + var url; + var _this = this; + function processSmd(smd){ + smd._baseUrl = new dojo._Url(location.href,url || '.') + ''; + _this._smd = smd; + + //generate the methods + for(var serviceName in _this._smd.services){ + _this[serviceName]=_this._generateService(serviceName, _this._smd.services[serviceName]); + + } + } + if(smd){ + //if the 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); + } + } + + if (options){this._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 + schema._idPrefix = serviceName +'/'; // schemas are minimally used to track the id prefixes for the different services + dojox.rpc.services[serviceName] = func; // register the service + schema._service = func; + func.serviceName = serviceName; + func._schema = schema; + + return func; + }, + + _executeMethod: function(method){ + var args = []; + var i; + for (i=1; i< arguments.length; i++){ + args.push(arguments[i]); + } + + var smd = this._smd; + if (method.parameters && method.parameters[0] && method.parameters[0].name && (args.length==1) && dojo.isObject(args[0])){ + // if it is the parameters are not named in the definition, then we should use ordered params, otherwise try to determine by parameters + args = args[0]; + // inherit root-level parameters + if (smd.parameters && smd.parameters[0]){ + for (i=0; i< smd.parameters.length; i++){ + if (smd.parameters[i]["name"] && smd.parameters[i]["default"]){ + args[smd.parameters[i]["name"]] = smd.parameters[i]["default"]; + } + } + } + } + if (dojo.isObject(this._options)){ + args = dojo.mixin(args, this._options); + } + + var envelope = method.envelope || smd.envelope || "NONE"; + var envDef = dojox.rpc.envelopeRegistry.match(envelope); + var schema = method._schema || method.returns; // serialize with the right schema for the context; + var request = envDef.serialize.apply(this, [smd, method, args]); + var contentType = (method.contentType || smd.contentType || request.contentType); + var isJson = (contentType + '').match(/application\/json/); + + // this allows to mandate synchronous behavior from elsewhere when necessary, this may need to be changed to be one-shot in FF3 new sync handling model + dojo.mixin(request,{sync : dojox.rpc._sync, + handleAs : isJson ? "json" : "text", + contentType : contentType, + target : request.target || dojox.rpc.getTarget(smd, method), + transport: method.transport || smd.transport || request.transport, + envelope: method.envelope || smd.envelope || request.envelope, + timeout: method.timeout || smd.timeout, + callbackParamName: method.callbackParamName || smd.callbackParamName, + preventCache: method.preventCache || smd.preventCache}); + + var deferred = (method.restMethod || dojox.rpc.transportRegistry.match(request.transport).fire).call(this,request); + deferred.addBoth(dojo.hitch(this,function(results){ + // if it is an application/json content type, than it should be handled as json + // we have to do conversion here instead of in XHR so that we can set the currentSchema before running it + results = envDef.deserialize.call(this,isJson ? dojox.rpc.resolveJson(results,schema) : results); + return results; + })); + return deferred; + } +}); + +dojox.rpc.getTarget = function(smd, method){ + + var dest=smd._baseUrl; + if (smd.target){ + dest = new dojo._Url(dest,smd.target) + ''; + } + if (method.target){ + dest = new dojo._Url(dest,method.target) + ''; + } + return dest; +} + +dojox.rpc.toNamed=function(method, args, strictParams){ + var i; + if (!dojo.isArray(args)){ + if (strictParams){ + //verify that all required parameters were supplied + for (i=0; i