Debian Codegraph @

dojox/store/OData.js

raw symbols SCIP
1 define([
2 "dojo/io-query",
3 "dojo/request",
4 "dojo/_base/lang",
5 "dojo/json",
6 "dojo/_base/declare",
7 "dojo/store/util/QueryResults"
8 ], function(ioQuery, request, lang, JSON, declare, QueryResults){
9 var base = null;
10 return declare(base, {
11 // summary:
12 // This is a basic store for RESTful cumminication with an OData
13 // server through JSON formatted data. It implements the
14 // dojo/store api and is designed to work specifically with WCF
15 // Microsoft SharePoint 2013 V2 services, though standard OData
16 // V2 endpoints should work as well.
17
18 // headers: Object
19 // Additional headers to pass in all requests to the server. These
20 // can be overridden by passing additional headers to calls to the
21 // store.
22 headers : {"MaxDataServiceVersion" : "2.0"},
23 // target: String
24 // The target base URL to use for all requests to the server. This
25 // string will be prepended to the id to generate the URL (relative
26 // or absolute) for requests sent to the server
27 target : "",
28 // idProperty: String
29 // Indicates the property to use as the identity property. The
30 // values of this property should be unique.
31 idProperty : "id",
32 // accepts: String
33 // Indicates the Accepts header to use for all requests to the
34 // server. This can be overridden by passing additional headers
35 // to calls to the store
36 accepts : "application/json;odata=verbose",
37 // childAttr: String
38 // Indicates the attribute that represents a given object's children.
39 // Used in hierarchical store operations, such as getChildren
40 childAttr: "children",
41
42 constructor: function(options){
43 declare.safeMixin(this, options);
44 },
45
46 get: function(id, options){
47 // summary:
48 // Retrieves an object by its identity. This will trigger a
49 // GET request to the server using the url `this.target +
50 // "("+id+")"`.
51 // id: String
52 // The identity to use to lookup the object
53 // options: Object?
54 // HTTP headers. For consistency with other methods, if a
55 // `headers` key exists on this object, it will be used to
56 // provide HTTP headers instead.
57 // returns: Object
58 // The object in the store that matches the given id.
59 options = options || {};
60 var headers = lang.mixin({ Accept: this.accepts }, this.headers, options.headers || options);
61 return request( this.target + "("+id+")", {
62 handleAs: "json",
63 headers: headers
64 }).then(function(data){
65 return data.d;
66 });
67 },
68
69 getIdentity: function(object){
70 // summary:
71 // Returns an object's identity
72 // object: Object
73 // The object to get the identity from
74 // returns: String
75 return object[this.idProperty];
76 },
77
78 put: function(object, options){
79 // summary:
80 // Updates an object. This will trigger a PUT or MERGE request
81 // to the server depending on if it is an incremental update
82 // object: Object
83 // The object to store.
84 // options: __PutDirectives?
85 // Additional metadata for storing the data. Includes an "id"
86 // property if a specific id is to be used.
87 // returns: dojo/_base/Deferred
88 options = options || {};
89 var id = this.getIdentity(object) || options[this.idProperty];
90 var url = id ? (this.target + "("+id+")") : this.target;
91 var headers = lang.mixin({
92 "Content-Type": "application/json;odata=verbose",
93 Accept: this.accepts
94 }, this.headers, options.headers);
95 if(id){
96 headers["X-HTTP-Method"] = options.overwrite ? "PUT" : "MERGE";
97 headers["IF-MATCH"] = options.overwrite ? "*" : (options.etag||"*")
98 }
99 return request.post( url, {
100 handleAs: "json",
101 data: JSON.stringify(object),
102 headers: headers
103 });
104 },
105
106 add: function(object, options){
107 // summary:
108 // Adds an object. This will trigger a POST request to the server
109 // object: Object
110 // The object to store.
111 // options: __PutDirectives?
112 // Additional metadata for storing the data. Includes an "id"
113 // property if a specific id is to be used.
114 options = options || {};
115 options.overwrite = false;
116 return this.put(object, options);
117 },
118
119 remove: function(id, options){
120 // summary:
121 // Deletes an object by its identity. This will trigger a DELETE
122 // request to the server.
123 // id: String
124 // The identity to use to delete the object
125 // options: __HeaderOptions?
126 // HTTP headers.
127 options = options || {};
128 return request.post(this.target + "("+id+")", {
129 headers: lang.mixin({
130 "IF-MATCH" : "*",
131 "X-HTTP-Method" : "DELETE"
132 }, this.headers, options.headers)
133 });
134 },
135
136 getFormDigest: function(){
137 // summary:
138 // Makes POST request to WCF endpoint based on target url to
139 // get a form digest authorization token
140 var i = this.target.indexOf("_vti_bin");
141 var url = this.target.slice(0, i)+"_api/contextinfo";
142 return request.post(url).then(function(xml){
143 return xml.substring(
144 xml.indexOf("<d:FormDigestValue>")+19,
145 xml.indexOf("</d:FormDigestValue>"));
146 });
147 },
148
149 getChildren: function(parent, options){
150 // summary:
151 // Retrieves the children of an object.
152 // parent: Object
153 // The object to find the children of.
154 // options: dojo/store/api/Store.QueryOptions?
155 // Additional options to apply to the retrieval of the children.
156 // returns: dojo/store/api/Store.QueryResults
157 // A result set of the children of the parent object.
158 var id = this.getIdentity(object) || options[this.idProperty];
159 return this.query({
160 "$filter" : this.idProperty+" eq "+id,
161 "$expand" : this.childAttr
162 }, options);
163 },
164
165 query: function(query, options){
166 // summary:
167 // Queries the store for objects. This will trigger a GET
168 // request to the server, with the query added as a query
169 // string that adheres to OData URI conventions.
170 // query: Object
171 // The query to use for retrieving objects from the store.
172 // Note that OData service operation keys are perfectly
173 // acceptable, e.g. `$filter`
174 // options: __QueryOptions?
175 // The optional arguments to apply to the resultset.
176 // returns: dojo/store/api/Store.QueryResults
177 // The results of the query, extended with iterative methods.
178 options = options || {};
179 var headers = lang.mixin({ Accept: this.accepts }, this.headers, options.headers);
180 // Sorting
181 if(options && options.sort){
182 query["$orderby"] = "";
183 var sort, i, len;
184 // loop through sort keys, build proper OData $orderby URI
185 for(i = 0, len=options.sort.length; i<len; i++){
186 sort = options.sort[i];
187 query["$orderby"] += ( i>0 ? "," : "") + encodeURIComponent(sort.attribute) +
188 (sort.descending ? " desc" : " asc");
189 }
190 }
191 // Pagination
192 if(options.start >= 0 || options.count >= 0){
193 query["$skip"] = options.start || 0;
194 query["$inlinecount"] = "allpages";
195 if("count" in options && options.count != Infinity){
196 query["$top"] = (options.count);
197 }
198 }
199 // Render a proper OData query string from JSON
200 query = this.buildQueryString(query);
201 var xhr = request(this.target + (query || ""), {
202 handleAs: "json",
203 headers: headers
204 });
205 // Because the returned OData format doesn't match what
206 // Dojo wants, let's chain another promise so we can grab
207 // the proper results array off the returned OData JSON
208 var results = xhr.then(function(data){
209 return data.d.results;
210 });
211 // Wrap the results (promise or not) with various utility methods
212 results = QueryResults(results);
213 // Set a promise to resolve the total count
214 results.total = xhr.then(function(data){
215 return data.d.__count;
216 });
217 return results;
218 },
219
220 buildQueryString: function(query){
221 // summary:
222 // Constructs appropriate OData V2 URI based on a given
223 // query object.
224 // query: Object
225 // The query to use for retrieving objects from the store.
226 // Note that OData service operation keys are perfectly
227 // acceptable, e.g. `$filter`.
228 // returns: String
229 // A OData V2 request URI in proper format
230 var filterStr = "";
231 for(var key in query){
232 // Only target non-system query options
233 if(query.hasOwnProperty(key) && key.indexOf("$") == -1){
234 var value = query[key]+"";
235 var i = value.indexOf("*");
236 // Handle wildcards
237 if(i != -1){
238 value = value.slice(i!=0 ? 0 : 1, value.length-(i!=0 ? 1 : 0));
239 if(value.length > 0){
240 filterStr += (filterStr.length == 0) ? "" : "and ";
241 filterStr += (i==0 ? "endswith" : "startswith")+"("+key+",'"+value+"')";
242 }
243 }
244 }
245 }
246 // Add filter string
247 if(filterStr.length > 0){
248 query["$filter"] = (query["$filter"] && query["$filter"].length>0) ?
249 (query["$filter"]+" and "+filterStr) : filterStr;
250 }
251 // Query-string-ify the query object properly
252 var hasQuestionMark = this.target.indexOf("?") > -1;
253 if(query && typeof query == "object"){
254 query = ioQuery.objectToQuery(query);
255 query = query ? (hasQuestionMark ? "&" : "?") + query: "";
256 }
257 return query;
258 }
259 });
260 });