source: Dev/branches/rest-dojo-ui/client/dojox/wire/ml/RestHandler.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.7 KB
Line 
1dojo.provide("dojox.wire.ml.RestHandler");
2
3dojo.require("dojox.wire._base");
4dojo.require("dojox.wire.ml.util");
5
6dojo.declare("dojox.wire.ml.RestHandler", null, {
7        //      summary:
8        //              A REST service handler
9        //      description:
10        //              This class serves as a base REST service.
11        //              Sub-classes may override _getContent() and _getResult() to handle
12        //              specific content types.
13        contentType: "text/plain",
14        handleAs: "text",
15
16        bind: function(method, parameters, deferred, url){
17                //      summary:
18                //              Call a service method with parameters.
19                //      description:
20                //              A service is called with a URL generated by _getUrl() and
21                //              an HTTP method specified with 'method'.
22                //              For "POST" and "PUT", a content is generated by _getContent().
23                //              When data is loaded, _getResult() is used to pass the result to
24                //              Deferred.callback().
25                //      method:
26                //              A method name
27                //      parameters:
28                //              An array of parameters
29                //      deferred:
30                //              'Deferred'
31                //      url:
32                //              A URL for the method
33                method = method.toUpperCase();
34                var self = this;
35                var args = {
36                        url: this._getUrl(method, parameters, url),
37                        contentType: this.contentType,
38                        handleAs: this.handleAs,
39                        headers: this.headers,
40                        preventCache: this.preventCache
41                };
42                var d = null;
43                if(method == "POST"){
44                        args.postData = this._getContent(method, parameters);
45                        d = dojo.rawXhrPost(args);
46                }else if(method == "PUT"){
47                        args.putData = this._getContent(method, parameters);
48                        d = dojo.rawXhrPut(args);
49                }else if(method == "DELETE"){
50                        d = dojo.xhrDelete(args);
51                }else{ // "GET"
52                        d = dojo.xhrGet(args);
53                }
54                d.addCallbacks(function(result){
55                        deferred.callback(self._getResult(result));
56                }, function(error){
57                        deferred.errback(error);
58                });
59        },
60
61        _getUrl: function(/*String*/method, /*Array*/parameters, /*String*/url){
62                //      summary:
63                //              Generate a URL
64                //      description:
65                //              If 'method' is "GET" or "DELETE", a query string is generated
66                //              from a query object specified to the first parameter in
67                //              'parameters' and appended to 'url'.
68                //              If 'url' contains variable seguments ("{parameter_name}"),
69                //              they are replaced with corresponding parameter values, instead.
70                //      method:
71                //              A method name
72                //      parameters:
73                //              An array of parameters
74                //      url:
75                //              A base URL
76                //      returns:
77                //              A URL
78                var query;
79                if(method == "GET" || method == "DELETE"){
80                        if(parameters.length > 0){
81                                query = parameters[0];
82                        }
83                }else{ // "POST" || "PUT"
84                        if(parameters.length > 1){
85                                query = parameters[1];
86                        }
87                }
88                if(query){
89                        var queryString = "";
90                        for(var name in query){
91                                var value = query[name];
92                                if(value){
93                                        value = encodeURIComponent(value);
94                                        var variable = "{" + name + "}";
95                                        var index = url.indexOf(variable);
96                                        if(index >= 0){ // encode in path
97                                                url = url.substring(0, index) + value + url.substring(index + variable.length);
98                                        }else{ // encode as query string
99                                                if(queryString){
100                                                        queryString += "&";
101                                                }
102                                                queryString += (name + "=" + value);
103                                        }
104                                }
105                        }
106                        if(queryString){
107                                url += "?" + queryString;
108                        }
109                }
110                return url; //String
111        },
112
113        _getContent: function(/*String*/method, /*Array*/parameters){
114                //      summary:
115                //              Generate a request content
116                //      description:
117                //              If 'method' is "POST" or "PUT", the first parameter in
118                //              'parameters' is returned.
119                //      method:
120                //              A method name
121                //      parameters:
122                //              An array of parameters
123                //      returns:
124                //              A request content
125                if(method == "POST" || method == "PUT"){
126                        return (parameters ? parameters[0] : null); //anything
127                }else{
128                        return null; //null
129                }
130        },
131
132        _getResult: function(/*anything*/data){
133                //      summary:
134                //              Extract a result
135                //      description:
136                //              A response data is returned as is.
137                //      data:
138                //              A response data returned by a service
139                //      returns:
140                //              A result object
141                return data; //anything
142        }
143});
Note: See TracBrowser for help on using the repository browser.