SMD: Pluggable Web Services
Date : 2008 03 20 Category : Tech & Development
PLAIN TEXT
JAVASCRIPT:
{target:"/jsonrpc", // this defines the URL to connect for the services transport:"POST", // We will use POST as the transport envelope:"JSON-RPC-1.2", // We will use JSON-RPC SMDVersion:"2.0", services: { add : { // define a service to add two numbers parameters: [ {name:"a",type:"number"}, // define the two parameters {name:"b",type:"number"}], returns:{"type":"number"} }, foo : { // nothing is required to be defined, all definitions are optional. //This service has nothing defined so it can take any parameters //and return any value }, getNews : { // we can redefine the target, transport, and envelope for specific services target: "/newsSearch", transport: "GET", envelope: "URL", parameters:[ { name: "query", type: "string", optional: false, default: "" } ], returns:{type:"array"} } }
var services = new dojox.rpc.Service("http://mydomain.com/mySMD"); var newsDeferred = services.getNews({query:"dojo"}); newsDeferred.addCallback(function(returnValue) { alert("A news item: " + returnValue[0]); });
{target:"/jsonrpc", // this defines the URL to connect for the services transport:"POST", // We will use POST as the transport envelope:"JSON-RPC-1.2", // We will use JSON-RPC SMDVersion:"2.0", services: { add : { // define a service to add two numbers parameters: [ {name:"a",type:"number"}, // define the two parameters {name:"b",type:"number"}], returns:{"type":"number"} }, foo : { // nothing is required to be defined, all definitions are optional. //This service has nothing defined so it can take any parameters //and return any value }, getNews : { // we can redefine the target, transport, and envelope for specific services target: "/newsSearch", transport: "GET", envelope: "URL", parameters:[ { name: "query", type: "string", optional: false, default: "" } ], returns:{type:"array"} } }
What is that? Does it look a little WSDL-y without the XML? This format is Service Mapping Description (SMD), and Kris Zyp talks about how it enables pluggable Web services.
With the schema about you can wire it together (example using Dojo):
PLAIN TEXT JAVASCRIPT:var services = new dojox.rpc.Service("http://mydomain.com/mySMD"); var newsDeferred = services.getNews({query:"dojo"}); newsDeferred.addCallback(function(returnValue) { alert("A news item: " + returnValue[0]); });
Neil Roberts asked about cross domain SMDs and Kris said:
Yes, we will use JSONP, although we use a special form, where the callback function will be derived directly from the name of the SMD, in order to allow for static creation of SMDs that can be accessed cross-domain.