simpleFetch.js 4.0 KB

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