Friday, June 27th, 2008
Working with Web Services with ease; dojo.data and the WikipediaStore
<p>Revin Guillen has posted about the Dojo dojo.data API and how you can layer access to Web services in a very elegant way.His example shows building access to Wikipedia (demo):
Dojo recently received a new data store that demonstrates exactly what we want: dojox.data.WikipediaStore. It does just what it sounds like, turning Wikipedia into a simple object you can query from your code. Building it with Dojo’s handy dojox.rpc package makes for a simple, compact, DRY implementation.
In only four steps:
- Create the web service object
- Declare the new data store, inheriting from ServiceStore
- Give it a fetch method
- Give it a _processResults method
The service descriptor looks like:
-
-
{
-
"SMDVersion": "2.0",
-
"id": "http://en.wikipedia.org/w/api.php",
-
"description": "Wikipedia API",
-
-
transport: "JSONP",
-
envelope: "URL",
-
additionalParameters: true,
-
target: "http://en.wikipedia.org/w/api.php",
-
parameters: [
-
{ name: "format", optional: false, "default": "json" }
-
],
-
-
services: {
-
query: {
-
parameters: [
-
{ name: "action", type: "string", "default": "parse" }
-
]
-
}
-
}
-
}
-
which you can use when you create the store:
-
-
dojo.require("dojo.io.script"); // for cross domain JSONP
-
dojo.require("dojox.rpc.Service");
-
-
dojo.addOnLoad(function(){
-
var mu = dojo.moduleUrl("dojox.rpc.SMDLibrary", "wikipedia.smd");
-
var wikipedia = new dojox.rpc.Service(mu);
-
-
wikipedia.query({
-
action: "parse",
-
page: "Main Page"
-
}).addCallback(this, function(article){
-
dojo.body().innerHTML = article.parse.text["*"];
-
});
-
});
-
Related Content:











Leave a comment