| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
headers : {"MaxDataServiceVersion" : "2.0"}, |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
target : "", |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
idProperty : "id", |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
accepts : "application/json;odata=verbose", |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
childAttr: "children", |
| 41 |
|
| 42 |
constructor: function(options){ |
| 43 |
declare.safeMixin(this, options); |
| 44 |
}, |
| 45 |
|
| 46 |
get: function(id, options){ |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
return object[this.idProperty]; |
| 76 |
}, |
| 77 |
|
| 78 |
put: function(object, options){ |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
options = options || {}; |
| 115 |
options.overwrite = false; |
| 116 |
return this.put(object, options); |
| 117 |
}, |
| 118 |
|
| 119 |
remove: function(id, options){ |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
options = options || {}; |
| 179 |
var headers = lang.mixin({ Accept: this.accepts }, this.headers, options.headers); |
| 180 |
|
| 181 |
if(options && options.sort){ |
| 182 |
query["$orderby"] = ""; |
| 183 |
var sort, i, len; |
| 184 |
|
| 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 |
|
| 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 |
|
| 200 |
query = this.buildQueryString(query); |
| 201 |
var xhr = request(this.target + (query || ""), { |
| 202 |
handleAs: "json", |
| 203 |
headers: headers |
| 204 |
}); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
var results = xhr.then(function(data){ |
| 209 |
return data.d.results; |
| 210 |
}); |
| 211 |
|
| 212 |
results = QueryResults(results); |
| 213 |
|
| 214 |
results.total = xhr.then(function(data){ |
| 215 |
return data.d.__count; |
| 216 |
}); |
| 217 |
return results; |
| 218 |
}, |
| 219 |
|
| 220 |
buildQueryString: function(query){ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
var filterStr = ""; |
| 231 |
for(var key in query){ |
| 232 |
|
| 233 |
if(query.hasOwnProperty(key) && key.indexOf("$") == -1){ |
| 234 |
var value = query[key]+""; |
| 235 |
var i = value.indexOf("*"); |
| 236 |
|
| 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 |
|
| 247 |
if(filterStr.length > 0){ |
| 248 |
query["$filter"] = (query["$filter"] && query["$filter"].length>0) ? |
| 249 |
(query["$filter"]+" and "+filterStr) : filterStr; |
| 250 |
} |
| 251 |
|
| 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 |
}); |