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