[483] | 1 | define("dojox/rpc/Rest", ["dojo", "dojox"], function(dojo, dojox) { |
---|
| 2 | // Note: This doesn't require dojox.rpc.Service, and if you want it you must require it |
---|
| 3 | // yourself, and you must load it prior to dojox.rpc.Rest. |
---|
| 4 | |
---|
| 5 | dojo.getObject("rpc.Rest", true, dojox); |
---|
| 6 | |
---|
| 7 | if(dojox.rpc && dojox.rpc.transportRegistry){ |
---|
| 8 | // register it as an RPC service if the registry is available |
---|
| 9 | dojox.rpc.transportRegistry.register( |
---|
| 10 | "REST", |
---|
| 11 | function(str){return str == "REST";}, |
---|
| 12 | { |
---|
| 13 | getExecutor : function(func,method,svc){ |
---|
| 14 | return new dojox.rpc.Rest( |
---|
| 15 | method.name, |
---|
| 16 | (method.contentType||svc._smd.contentType||"").match(/json|javascript/), // isJson |
---|
| 17 | null, |
---|
| 18 | function(id, args){ |
---|
| 19 | var request = svc._getRequest(method,[id]); |
---|
| 20 | request.url= request.target + (request.data ? '?'+ request.data : ''); |
---|
| 21 | if(args && (args.start >= 0 || args.count >= 0)){ |
---|
| 22 | request.headers = request.headers || {}; |
---|
| 23 | request.headers.Range = "items=" + (args.start || '0') + '-' + |
---|
| 24 | (("count" in args && args.count != Infinity) ? |
---|
| 25 | (args.count + (args.start || 0) - 1) : ''); |
---|
| 26 | } |
---|
| 27 | return request; |
---|
| 28 | } |
---|
| 29 | ); |
---|
| 30 | } |
---|
| 31 | } |
---|
| 32 | ); |
---|
| 33 | } |
---|
| 34 | var drr; |
---|
| 35 | |
---|
| 36 | function index(deferred, service, range, id){ |
---|
| 37 | deferred.addCallback(function(result){ |
---|
| 38 | if(deferred.ioArgs.xhr && range){ |
---|
| 39 | // try to record the total number of items from the range header |
---|
| 40 | range = deferred.ioArgs.xhr.getResponseHeader("Content-Range"); |
---|
| 41 | deferred.fullLength = range && (range=range.match(/\/(.*)/)) && parseInt(range[1]); |
---|
| 42 | } |
---|
| 43 | return result; |
---|
| 44 | }); |
---|
| 45 | return deferred; |
---|
| 46 | } |
---|
| 47 | drr = dojox.rpc.Rest = function(/*String*/path, /*Boolean?*/isJson, /*Object?*/schema, /*Function?*/getRequest){ |
---|
| 48 | // summary: |
---|
| 49 | // This provides a HTTP REST service with full range REST verbs include PUT,POST, and DELETE. |
---|
| 50 | // description: |
---|
| 51 | // A normal GET query is done by using the service directly: |
---|
| 52 | // | var restService = dojox.rpc.Rest("Project"); |
---|
| 53 | // | restService("4"); |
---|
| 54 | // This will do a GET for the URL "/Project/4". |
---|
| 55 | // | restService.put("4","new content"); |
---|
| 56 | // This will do a PUT to the URL "/Project/4" with the content of "new content". |
---|
| 57 | // You can also use the SMD service to generate a REST service: |
---|
| 58 | // | var services = dojox.rpc.Service({services: {myRestService: {transport: "REST",... |
---|
| 59 | // | services.myRestService("parameters"); |
---|
| 60 | // |
---|
| 61 | // The modifying methods can be called as sub-methods of the rest service method like: |
---|
| 62 | // | services.myRestService.put("parameters","data to put in resource"); |
---|
| 63 | // | services.myRestService.post("parameters","data to post to the resource"); |
---|
| 64 | // | services.myRestService['delete']("parameters"); |
---|
| 65 | |
---|
| 66 | var service; |
---|
| 67 | // it should be in the form /Table/ |
---|
| 68 | service = function(id, args){ |
---|
| 69 | return drr._get(service, id, args); |
---|
| 70 | }; |
---|
| 71 | service.isJson = isJson; |
---|
| 72 | service._schema = schema; |
---|
| 73 | // cache: |
---|
| 74 | // This is an object that provides indexing service |
---|
| 75 | // This can be overriden to take advantage of more complex referencing/indexing |
---|
| 76 | // schemes |
---|
| 77 | service.cache = { |
---|
| 78 | serialize: isJson ? ((dojox.json && dojox.json.ref) || dojo).toJson : function(result){ |
---|
| 79 | return result; |
---|
| 80 | } |
---|
| 81 | }; |
---|
| 82 | // the default XHR args creator: |
---|
| 83 | service._getRequest = getRequest || function(id, args){ |
---|
| 84 | if(dojo.isObject(id)){ |
---|
| 85 | id = dojo.objectToQuery(id); |
---|
| 86 | id = id ? "?" + id: ""; |
---|
| 87 | } |
---|
| 88 | if(args && args.sort && !args.queryStr){ |
---|
| 89 | id += (id ? "&" : "?") + "sort(" |
---|
| 90 | for(var i = 0; i<args.sort.length; i++){ |
---|
| 91 | var sort = args.sort[i]; |
---|
| 92 | id += (i > 0 ? "," : "") + (sort.descending ? '-' : '+') + encodeURIComponent(sort.attribute); |
---|
| 93 | } |
---|
| 94 | id += ")"; |
---|
| 95 | } |
---|
| 96 | var request = { |
---|
| 97 | url: path + (id == null ? "" : id), |
---|
| 98 | handleAs: isJson ? 'json' : 'text', |
---|
| 99 | contentType: isJson ? 'application/json' : 'text/plain', |
---|
| 100 | sync: dojox.rpc._sync, |
---|
| 101 | headers: { |
---|
| 102 | Accept: isJson ? 'application/json,application/javascript' : '*/*' |
---|
| 103 | } |
---|
| 104 | }; |
---|
| 105 | if(args && (args.start >= 0 || args.count >= 0)){ |
---|
| 106 | request.headers.Range = "items=" + (args.start || '0') + '-' + |
---|
| 107 | (("count" in args && args.count != Infinity) ? |
---|
| 108 | (args.count + (args.start || 0) - 1) : ''); |
---|
| 109 | } |
---|
| 110 | dojox.rpc._sync = false; |
---|
| 111 | return request; |
---|
| 112 | }; |
---|
| 113 | // each calls the event handler |
---|
| 114 | function makeRest(name){ |
---|
| 115 | service[name] = function(id,content){ |
---|
| 116 | return drr._change(name,service,id,content); // the last parameter is to let the OfflineRest know where to store the item |
---|
| 117 | }; |
---|
| 118 | } |
---|
| 119 | makeRest('put'); |
---|
| 120 | makeRest('post'); |
---|
| 121 | makeRest('delete'); |
---|
| 122 | // record the REST services for later lookup |
---|
| 123 | service.servicePath = path; |
---|
| 124 | return service; |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | drr._index={};// the map of all indexed objects that have gone through REST processing |
---|
| 128 | drr._timeStamps={}; |
---|
| 129 | // these do the actual requests |
---|
| 130 | drr._change = function(method,service,id,content){ |
---|
| 131 | // this is called to actually do the put, post, and delete |
---|
| 132 | var request = service._getRequest(id); |
---|
| 133 | request[method+"Data"] = content; |
---|
| 134 | return index(dojo.xhr(method.toUpperCase(),request,true),service); |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | drr._get= function(service,id, args){ |
---|
| 138 | args = args || {}; |
---|
| 139 | // this is called to actually do the get |
---|
| 140 | return index(dojo.xhrGet(service._getRequest(id, args)), service, (args.start >= 0 || args.count >= 0), id); |
---|
| 141 | }; |
---|
| 142 | |
---|
| 143 | return drr; |
---|
| 144 | }); |
---|