| 1 |
define(["dojo", "dojox", "dojo/data/util/sorter", "dojo/string"], function(dojo, dojox) { |
| 2 |
|
| 3 |
return dojo.declare("dojox.data.QueryReadStore", null, { |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
url:"", |
| 55 |
requestMethod:"get", |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
_className:"dojox.data.QueryReadStore", |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
_items:[], |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
_lastServerQuery:null, |
| 74 |
|
| 75 |
|
| 76 |
_numRows:-1, |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
lastRequestHash:null, |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
doClientPaging:false, |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
doClientSorting:false, |
| 91 |
|
| 92 |
|
| 93 |
_itemsByIdentity:null, |
| 94 |
|
| 95 |
|
| 96 |
_identifier:null, |
| 97 |
|
| 98 |
_features: {'dojo.data.api.Read':true, 'dojo.data.api.Identity':true}, |
| 99 |
|
| 100 |
_labelAttr: "label", |
| 101 |
|
| 102 |
constructor: function( params){ |
| 103 |
dojo.mixin(this,params); |
| 104 |
}, |
| 105 |
|
| 106 |
getValue: function( item, attribute, defaultValue){ |
| 107 |
|
| 108 |
|
| 109 |
this._assertIsItem(item); |
| 110 |
if(!dojo.isString(attribute)){ |
| 111 |
throw new Error(this._className+".getValue(): Invalid attribute, string expected!"); |
| 112 |
} |
| 113 |
if(!this.hasAttribute(item, attribute)){ |
| 114 |
|
| 115 |
|
| 116 |
if(defaultValue){ |
| 117 |
return defaultValue; |
| 118 |
} |
| 119 |
} |
| 120 |
return item.i[attribute]; |
| 121 |
}, |
| 122 |
|
| 123 |
getValues: function( item, attribute){ |
| 124 |
this._assertIsItem(item); |
| 125 |
var ret = []; |
| 126 |
if(this.hasAttribute(item, attribute)){ |
| 127 |
ret.push(item.i[attribute]); |
| 128 |
} |
| 129 |
return ret; |
| 130 |
}, |
| 131 |
|
| 132 |
getAttributes: function( item){ |
| 133 |
this._assertIsItem(item); |
| 134 |
var ret = []; |
| 135 |
for(var i in item.i){ |
| 136 |
ret.push(i); |
| 137 |
} |
| 138 |
return ret; |
| 139 |
}, |
| 140 |
|
| 141 |
hasAttribute: function( item, attribute){ |
| 142 |
|
| 143 |
|
| 144 |
return this.isItem(item) && typeof item.i[attribute]!="undefined"; |
| 145 |
}, |
| 146 |
|
| 147 |
containsValue: function( item, attribute, value){ |
| 148 |
var values = this.getValues(item, attribute); |
| 149 |
var len = values.length; |
| 150 |
for(var i=0; i<len; i++){ |
| 151 |
if(values[i] == value){ |
| 152 |
return true; |
| 153 |
} |
| 154 |
} |
| 155 |
return false; |
| 156 |
}, |
| 157 |
|
| 158 |
isItem: function( something){ |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if(something){ |
| 177 |
return typeof something.r != "undefined" && something.r == this; |
| 178 |
} |
| 179 |
return false; |
| 180 |
}, |
| 181 |
|
| 182 |
isItemLoaded: function( something){ |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
return this.isItem(something); |
| 187 |
}, |
| 188 |
|
| 189 |
loadItem: function( args){ |
| 190 |
if(this.isItemLoaded(args.item)){ |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
}, |
| 195 |
|
| 196 |
fetch:function( request){ |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
request = request || {}; |
| 204 |
if(!request.store){ |
| 205 |
request.store = this; |
| 206 |
} |
| 207 |
var self = this; |
| 208 |
|
| 209 |
var _errorHandler = function(errorData, requestObject){ |
| 210 |
if(requestObject.onError){ |
| 211 |
var scope = requestObject.scope || dojo.global; |
| 212 |
requestObject.onError.call(scope, errorData, requestObject); |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
var _fetchHandler = function(items, requestObject, numRows){ |
| 217 |
var oldAbortFunction = requestObject.abort || null; |
| 218 |
var aborted = false; |
| 219 |
|
| 220 |
var startIndex = requestObject.start?requestObject.start:0; |
| 221 |
if(self.doClientPaging == false){ |
| 222 |
|
| 223 |
startIndex = 0; |
| 224 |
} |
| 225 |
var endIndex = requestObject.count?(startIndex + requestObject.count):items.length; |
| 226 |
|
| 227 |
requestObject.abort = function(){ |
| 228 |
aborted = true; |
| 229 |
if(oldAbortFunction){ |
| 230 |
oldAbortFunction.call(requestObject); |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
var scope = requestObject.scope || dojo.global; |
| 235 |
if(!requestObject.store){ |
| 236 |
requestObject.store = self; |
| 237 |
} |
| 238 |
if(requestObject.onBegin){ |
| 239 |
requestObject.onBegin.call(scope, numRows, requestObject); |
| 240 |
} |
| 241 |
if(requestObject.sort && self.doClientSorting){ |
| 242 |
items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self)); |
| 243 |
} |
| 244 |
if(requestObject.onItem){ |
| 245 |
for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){ |
| 246 |
var item = items[i]; |
| 247 |
if(!aborted){ |
| 248 |
requestObject.onItem.call(scope, item, requestObject); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
if(requestObject.onComplete && !aborted){ |
| 253 |
var subset = null; |
| 254 |
if(!requestObject.onItem){ |
| 255 |
subset = items.slice(startIndex, endIndex); |
| 256 |
} |
| 257 |
requestObject.onComplete.call(scope, subset, requestObject); |
| 258 |
} |
| 259 |
}; |
| 260 |
this._fetchItems(request, _fetchHandler, _errorHandler); |
| 261 |
return request; |
| 262 |
}, |
| 263 |
|
| 264 |
getFeatures: function(){ |
| 265 |
return this._features; |
| 266 |
}, |
| 267 |
|
| 268 |
close: function( request){ |
| 269 |
|
| 270 |
}, |
| 271 |
|
| 272 |
getLabel: function( item){ |
| 273 |
|
| 274 |
|
| 275 |
if(this._labelAttr && this.isItem(item)){ |
| 276 |
return this.getValue(item, this._labelAttr); |
| 277 |
} |
| 278 |
return undefined; |
| 279 |
}, |
| 280 |
|
| 281 |
getLabelAttributes: function( item){ |
| 282 |
|
| 283 |
|
| 284 |
if(this._labelAttr){ |
| 285 |
return [this._labelAttr]; |
| 286 |
} |
| 287 |
return null; |
| 288 |
}, |
| 289 |
|
| 290 |
_xhrFetchHandler: function(data, request, fetchHandler, errorHandler){ |
| 291 |
data = this._filterResponse(data); |
| 292 |
if(data.label){ |
| 293 |
this._labelAttr = data.label; |
| 294 |
} |
| 295 |
var numRows = data.numRows || -1; |
| 296 |
|
| 297 |
this._items = []; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
dojo.forEach(data.items,function(e){ |
| 302 |
this._items.push({i:e, r:this}); |
| 303 |
},this); |
| 304 |
|
| 305 |
var identifier = data.identifier; |
| 306 |
this._itemsByIdentity = {}; |
| 307 |
if(identifier){ |
| 308 |
this._identifier = identifier; |
| 309 |
var i; |
| 310 |
for(i = 0; i < this._items.length; ++i){ |
| 311 |
var item = this._items[i].i; |
| 312 |
var identity = item[identifier]; |
| 313 |
if(!this._itemsByIdentity[identity]){ |
| 314 |
this._itemsByIdentity[identity] = item; |
| 315 |
}else{ |
| 316 |
throw new Error(this._className+": The json data as specified by: [" + this.url + "] is malformed. Items within the list have identifier: [" + identifier + "]. Value collided: [" + identity + "]"); |
| 317 |
} |
| 318 |
} |
| 319 |
}else{ |
| 320 |
this._identifier = Number; |
| 321 |
for(i = 0; i < this._items.length; ++i){ |
| 322 |
this._items[i].n = i; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
numRows = this._numRows = (numRows === -1) ? this._items.length : numRows; |
| 329 |
fetchHandler(this._items, request, numRows); |
| 330 |
this._numRows = numRows; |
| 331 |
}, |
| 332 |
|
| 333 |
_fetchItems: function(request, fetchHandler, errorHandler){ |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
var serverQuery = request.serverQuery || request.query || {}; |
| 364 |
|
| 365 |
if(!this.doClientPaging){ |
| 366 |
serverQuery.start = request.start || 0; |
| 367 |
|
| 368 |
if(request.count){ |
| 369 |
serverQuery.count = request.count; |
| 370 |
} |
| 371 |
} |
| 372 |
if(!this.doClientSorting && request.sort){ |
| 373 |
var sortInfo = []; |
| 374 |
dojo.forEach(request.sort, function(sort){ |
| 375 |
if(sort && sort.attribute){ |
| 376 |
sortInfo.push((sort.descending ? "-" : "") + sort.attribute); |
| 377 |
} |
| 378 |
}); |
| 379 |
serverQuery.sort = sortInfo.join(','); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
if(this.doClientPaging && this._lastServerQuery !== null && |
| 384 |
dojo.toJson(serverQuery) == dojo.toJson(this._lastServerQuery) |
| 385 |
){ |
| 386 |
this._numRows = (this._numRows === -1) ? this._items.length : this._numRows; |
| 387 |
fetchHandler(this._items, request, this._numRows); |
| 388 |
}else{ |
| 389 |
var xhrFunc = this.requestMethod.toLowerCase() == "post" ? dojo.xhrPost : dojo.xhrGet; |
| 390 |
var xhrHandler = xhrFunc({url:this.url, handleAs:"json-comment-optional", content:serverQuery, failOk: true}); |
| 391 |
request.abort = function(){ |
| 392 |
xhrHandler.cancel(); |
| 393 |
}; |
| 394 |
xhrHandler.addCallback(dojo.hitch(this, function(data){ |
| 395 |
this._xhrFetchHandler(data, request, fetchHandler, errorHandler); |
| 396 |
})); |
| 397 |
xhrHandler.addErrback(function(error){ |
| 398 |
errorHandler(error, request); |
| 399 |
}); |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
this.lastRequestHash = new Date().getTime()+"-"+String(Math.random()).substring(2); |
| 404 |
this._lastServerQuery = dojo.mixin({}, serverQuery); |
| 405 |
} |
| 406 |
}, |
| 407 |
|
| 408 |
_filterResponse: function(data){ |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
return data; |
| 416 |
}, |
| 417 |
|
| 418 |
_assertIsItem: function( item){ |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
if(!this.isItem(item)){ |
| 425 |
throw new Error(this._className+": Invalid item argument."); |
| 426 |
} |
| 427 |
}, |
| 428 |
|
| 429 |
_assertIsAttribute: function( attribute){ |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if(typeof attribute !== "string"){ |
| 435 |
throw new Error(this._className+": Invalid attribute argument ('"+attribute+"')."); |
| 436 |
} |
| 437 |
}, |
| 438 |
|
| 439 |
fetchItemByIdentity: function( keywordArgs){ |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if(this._itemsByIdentity){ |
| 447 |
var item = this._itemsByIdentity[keywordArgs.identity]; |
| 448 |
if(!(item === undefined)){ |
| 449 |
if(keywordArgs.onItem){ |
| 450 |
var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global; |
| 451 |
keywordArgs.onItem.call(scope, {i:item, r:this}); |
| 452 |
} |
| 453 |
return; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
var _errorHandler = function(errorData, requestObject){ |
| 460 |
var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global; |
| 461 |
if(keywordArgs.onError){ |
| 462 |
keywordArgs.onError.call(scope, errorData); |
| 463 |
} |
| 464 |
}; |
| 465 |
|
| 466 |
|
| 467 |
var _fetchHandler = function(items, requestObject){ |
| 468 |
var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global; |
| 469 |
try{ |
| 470 |
|
| 471 |
var item = null; |
| 472 |
if(items && items.length == 1){ |
| 473 |
item = items[0]; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if(keywordArgs.onItem){ |
| 479 |
keywordArgs.onItem.call(scope, item); |
| 480 |
} |
| 481 |
}catch(error){ |
| 482 |
if(keywordArgs.onError){ |
| 483 |
keywordArgs.onError.call(scope, error); |
| 484 |
} |
| 485 |
} |
| 486 |
}; |
| 487 |
|
| 488 |
|
| 489 |
var request = {serverQuery:{id:keywordArgs.identity}}; |
| 490 |
|
| 491 |
|
| 492 |
this._fetchItems(request, _fetchHandler, _errorHandler); |
| 493 |
}, |
| 494 |
|
| 495 |
getIdentity: function( item){ |
| 496 |
|
| 497 |
|
| 498 |
var identifier = null; |
| 499 |
if(this._identifier === Number){ |
| 500 |
identifier = item.n; |
| 501 |
}else{ |
| 502 |
identifier = item.i[this._identifier]; |
| 503 |
} |
| 504 |
return identifier; |
| 505 |
}, |
| 506 |
|
| 507 |
getIdentityAttributes: function( item){ |
| 508 |
|
| 509 |
|
| 510 |
return [this._identifier]; |
| 511 |
} |
| 512 |
}); |
| 513 |
|
| 514 |
}); |