]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/data/util/simpleFetch.js
upgrade Dojo to 1.6.1
[tt-rss.git] / lib / dojo / data / util / simpleFetch.js
CommitLineData
2f01fe57 1/*
81bea17a 2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
2f01fe57
AD
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5*/
6
7
a089699c
AD
8if(!dojo._hasResource["dojo.data.util.simpleFetch"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9dojo._hasResource["dojo.data.util.simpleFetch"] = true;
2f01fe57
AD
10dojo.provide("dojo.data.util.simpleFetch");
11dojo.require("dojo.data.util.sorter");
a089699c 12
81bea17a
AD
13dojo.getObject("data.util.simpleFetch", true, dojo);
14
a089699c
AD
15dojo.data.util.simpleFetch.fetch = function(/* Object? */ request){
16 // summary:
17 // The simpleFetch mixin is designed to serve as a set of function(s) that can
81bea17a
AD
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()
a089699c
AD
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
81bea17a 23 // set. For datastores that mixin simpleFetch, simpleFetch
a089699c
AD
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(),
81bea17a
AD
27 // but should instead implement a _fetchItems() method. The _fetchItems()
28 // method takes three arguments, the keywordArgs object that was passed
a089699c
AD
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
81bea17a 32 // start, count, onBegin, onItem, onComplete, onError, sort, and scope.
a089699c 33 // The _fetchItems() method needs to correctly handle any other keywordArgs
81bea17a
AD
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
a089699c
AD
39 // that as the request object to the handler.
40 //
41 // For more information on this specific function, see dojo.data.api.Read.fetch()
42 request = request || {};
43 if(!request.store){
44 request.store = this;
45 }
46 var self = this;
47
48 var _errorHandler = function(errorData, requestObject){
49 if(requestObject.onError){
50 var scope = requestObject.scope || dojo.global;
51 requestObject.onError.call(scope, errorData, requestObject);
52 }
53 };
54
55 var _fetchHandler = function(items, requestObject){
56 var oldAbortFunction = requestObject.abort || null;
57 var aborted = false;
58
59 var startIndex = requestObject.start?requestObject.start:0;
60 var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length;
61
62 requestObject.abort = function(){
63 aborted = true;
64 if(oldAbortFunction){
65 oldAbortFunction.call(requestObject);
66 }
67 };
68
69 var scope = requestObject.scope || dojo.global;
70 if(!requestObject.store){
71 requestObject.store = self;
72 }
73 if(requestObject.onBegin){
74 requestObject.onBegin.call(scope, items.length, requestObject);
75 }
76 if(requestObject.sort){
77 items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self));
78 }
79 if(requestObject.onItem){
80 for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){
81 var item = items[i];
82 if(!aborted){
83 requestObject.onItem.call(scope, item, requestObject);
84 }
85 }
86 }
87 if(requestObject.onComplete && !aborted){
88 var subset = null;
89 if(!requestObject.onItem){
90 subset = items.slice(startIndex, endIndex);
91 }
92 requestObject.onComplete.call(scope, subset, requestObject);
93 }
94 };
95 this._fetchItems(request, _fetchHandler, _errorHandler);
96 return request; // Object
2f01fe57 97};
a089699c 98
2f01fe57 99}