]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/data/util/simpleFetch.js
b3f859b4899ff46e4521da6bd6819af06def9796
2 Copyright (c) 2004-2011, 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
8 if(!dojo
._hasResource
["dojo.data.util.simpleFetch"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo.data.util.simpleFetch"] = true;
10 dojo
.provide("dojo.data.util.simpleFetch");
11 dojo
.require("dojo.data.util.sorter");
13 dojo
.getObject("data.util.simpleFetch", true, dojo
);
15 dojo
.data
.util
.simpleFetch
.fetch = function(/* Object? */ request
){
17 // The simpleFetch mixin is designed to serve as a set of function(s) that can
18 // be mixed into other datastore implementations to accelerate their development.
19 // The simpleFetch mixin should work well for any datastore that can respond to a _fetchItems()
20 // call by returning an array of all the found items that matched the query. The simpleFetch mixin
21 // is not designed to work for datastores that respond to a fetch() call by incrementally
22 // loading items, or sequentially loading partial batches of the result
23 // set. For datastores that mixin simpleFetch, simpleFetch
24 // implements a fetch method that automatically handles eight of the fetch()
25 // arguments -- onBegin, onItem, onComplete, onError, start, count, sort and scope
26 // The class mixing in simpleFetch should not implement fetch(),
27 // but should instead implement a _fetchItems() method. The _fetchItems()
28 // method takes three arguments, the keywordArgs object that was passed
29 // to fetch(), a callback function to be called when the result array is
30 // available, and an error callback to be called if something goes wrong.
31 // The _fetchItems() method should ignore any keywordArgs parameters for
32 // start, count, onBegin, onItem, onComplete, onError, sort, and scope.
33 // The _fetchItems() method needs to correctly handle any other keywordArgs
34 // parameters, including the query parameter and any optional parameters
35 // (such as includeChildren). The _fetchItems() method should create an array of
36 // result items and pass it to the fetchHandler along with the original request object
37 // -- or, the _fetchItems() method may, if it wants to, create an new request object
38 // with other specifics about the request that are specific to the datastore and pass
39 // that as the request object to the handler.
41 // For more information on this specific function, see dojo.data.api.Read.fetch()
42 request
= request
|| {};
48 var _errorHandler = function(errorData
, requestObject
){
49 if(requestObject
.onError
){
50 var scope
= requestObject
.scope
|| dojo
.global
;
51 requestObject
.onError
.call(scope
, errorData
, requestObject
);
55 var _fetchHandler = function(items
, requestObject
){
56 var oldAbortFunction
= requestObject
.abort
|| null;
59 var startIndex
= requestObject
.start
?requestObject
.start
:0;
60 var endIndex
= (requestObject
.count
&& (requestObject
.count
!== Infinity
))?(startIndex
+ requestObject
.count
):items
.length
;
62 requestObject
.abort = function(){
65 oldAbortFunction
.call(requestObject
);
69 var scope
= requestObject
.scope
|| dojo
.global
;
70 if(!requestObject
.store
){
71 requestObject
.store
= self
;
73 if(requestObject
.onBegin
){
74 requestObject
.onBegin
.call(scope
, items
.length
, requestObject
);
76 if(requestObject
.sort
){
77 items
.sort(dojo
.data
.util
.sorter
.createSortFunction(requestObject
.sort
, self
));
79 if(requestObject
.onItem
){
80 for(var i
= startIndex
; (i
< items
.length
) && (i
< endIndex
); ++i
){
83 requestObject
.onItem
.call(scope
, item
, requestObject
);
87 if(requestObject
.onComplete
&& !aborted
){
89 if(!requestObject
.onItem
){
90 subset
= items
.slice(startIndex
, endIndex
);
92 requestObject
.onComplete
.call(scope
, subset
, requestObject
);
95 this._fetchItems(request
, _fetchHandler
, _errorHandler
);
96 return request
; // Object