simpleFetch.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. define("dojo/data/util/simpleFetch", ["../../_base/lang", "../../_base/window", "./sorter"],
  2. function(lang, winUtil, sorter) {
  3. // module:
  4. // dojo/data/util/simpleFetch
  5. // summary:
  6. // TODOC
  7. var simpleFetch = lang.getObject("dojo.data.util.simpleFetch", true);
  8. simpleFetch.fetch = function(/* Object? */ request){
  9. // summary:
  10. // The simpleFetch mixin is designed to serve as a set of function(s) that can
  11. // be mixed into other datastore implementations to accelerate their development.
  12. // The simpleFetch mixin should work well for any datastore that can respond to a _fetchItems()
  13. // call by returning an array of all the found items that matched the query. The simpleFetch mixin
  14. // is not designed to work for datastores that respond to a fetch() call by incrementally
  15. // loading items, or sequentially loading partial batches of the result
  16. // set. For datastores that mixin simpleFetch, simpleFetch
  17. // implements a fetch method that automatically handles eight of the fetch()
  18. // arguments -- onBegin, onItem, onComplete, onError, start, count, sort and scope
  19. // The class mixing in simpleFetch should not implement fetch(),
  20. // but should instead implement a _fetchItems() method. The _fetchItems()
  21. // method takes three arguments, the keywordArgs object that was passed
  22. // to fetch(), a callback function to be called when the result array is
  23. // available, and an error callback to be called if something goes wrong.
  24. // The _fetchItems() method should ignore any keywordArgs parameters for
  25. // start, count, onBegin, onItem, onComplete, onError, sort, and scope.
  26. // The _fetchItems() method needs to correctly handle any other keywordArgs
  27. // parameters, including the query parameter and any optional parameters
  28. // (such as includeChildren). The _fetchItems() method should create an array of
  29. // result items and pass it to the fetchHandler along with the original request object
  30. // -- or, the _fetchItems() method may, if it wants to, create an new request object
  31. // with other specifics about the request that are specific to the datastore and pass
  32. // that as the request object to the handler.
  33. //
  34. // For more information on this specific function, see dojo.data.api.Read.fetch()
  35. request = request || {};
  36. if(!request.store){
  37. request.store = this;
  38. }
  39. var self = this;
  40. var _errorHandler = function(errorData, requestObject){
  41. if(requestObject.onError){
  42. var scope = requestObject.scope || winUtil.global;
  43. requestObject.onError.call(scope, errorData, requestObject);
  44. }
  45. };
  46. var _fetchHandler = function(items, requestObject){
  47. var oldAbortFunction = requestObject.abort || null;
  48. var aborted = false;
  49. var startIndex = requestObject.start?requestObject.start:0;
  50. var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length;
  51. requestObject.abort = function(){
  52. aborted = true;
  53. if(oldAbortFunction){
  54. oldAbortFunction.call(requestObject);
  55. }
  56. };
  57. var scope = requestObject.scope || winUtil.global;
  58. if(!requestObject.store){
  59. requestObject.store = self;
  60. }
  61. if(requestObject.onBegin){
  62. requestObject.onBegin.call(scope, items.length, requestObject);
  63. }
  64. if(requestObject.sort){
  65. items.sort(sorter.createSortFunction(requestObject.sort, self));
  66. }
  67. if(requestObject.onItem){
  68. for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){
  69. var item = items[i];
  70. if(!aborted){
  71. requestObject.onItem.call(scope, item, requestObject);
  72. }
  73. }
  74. }
  75. if(requestObject.onComplete && !aborted){
  76. var subset = null;
  77. if(!requestObject.onItem){
  78. subset = items.slice(startIndex, endIndex);
  79. }
  80. requestObject.onComplete.call(scope, subset, requestObject);
  81. }
  82. };
  83. this._fetchItems(request, _fetchHandler, _errorHandler);
  84. return request; // Object
  85. };
  86. return simpleFetch;
  87. });