Pseudo-RESTful
I have to admit it: I spent the last week rolling my own data store that handles lazy loading that I thought was going to be the greatest thing since… well anyway. Then I read this , and realized that there was no need to write almost any of what I had written. I hate when that happens.
In any case, the new JsonRestStore does just about everything I wanted to do, only more efficiently. My only problem is that my server, which I have no control over, has blocked “PUT” and “DELETE”. But really, that seems minor. I mean, couldn’t we just include a flag to our REST server telling it what action we are expecting? Yes. We can.
A detailed look into dojox.rpc.Rest reveals that the _change method is where the final call to xhr occurs. Now I know this is unsafe since this is a private method, but its a good quick fix for now. And perhaps I can convince the dojo folks after 1.2 is official that they should support this kind of behavior for “Pseudo-REST” services, right out of the box.
Here is the code.
/** * * uses post for all post/put/delete, but adds and attribute called action * * @author maulin * */ dojo.provide("medryx.stores.ModifiedRest"); dojo.require("dojox.rpc.Rest"); (function() { //copied verbatim from dojox.rpc.Rest -- it is local in scope inside Rest //and called from the _change method, so I need it (for now) function index(deferred, service, range, id){ deferred.addCallback(function(result){ if(id==""){ service._rootQueries.push(result); } if(range){ // try to record the total number of items from the range header range = deferred.ioArgs.xhr.getResponseHeader("Content-Range"); deferred.fullLength = range && (range=range.match(/\/(.*)/|>)) && parseInt(range[1]); } return service.cache.intake(result,id); }); return deferred; } //"over-rides" the actual xhr call to use POST and add action attribute to querystring dojox.rpc.Rest._change = function(method,service,id,content){ // this is called to actually do the put, post, and delete var request = service._getRequest(id); request[method+"Data"] = content; //ADDED THE FOLLOWING TWO LINES request.data += "&action=" + method.toLowerCase(); request.url += (request.url.indexOf('?') > 0 ? "&" : "?" ) + "action=" + method.toLowerCase(); //CHANGED THIS LINE TO ALWAYS USE POST return index(dojo.xhr("POST",request,true),service); }; })();