2150 |
mathias |
1 |
if(!dojo._hasResource["dojo.data.util.simpleFetch"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojo.data.util.simpleFetch"] = true;
|
|
|
3 |
dojo.provide("dojo.data.util.simpleFetch");
|
|
|
4 |
dojo.require("dojo.data.util.sorter");
|
|
|
5 |
|
|
|
6 |
dojo.data.util.simpleFetch.fetch = function(/* Object? */ request){
|
|
|
7 |
// summary:
|
|
|
8 |
// The simpleFetch mixin is designed to serve as a set of function(s) that can
|
|
|
9 |
// be mixed into other datastore implementations to accelerate their development.
|
|
|
10 |
// The simpleFetch mixin should work well for any datastore that can respond to a _fetchItems()
|
|
|
11 |
// call by returning an array of all the found items that matched the query. The simpleFetch mixin
|
|
|
12 |
// is not designed to work for datastores that respond to a fetch() call by incrementally
|
|
|
13 |
// loading items, or sequentially loading partial batches of the result
|
|
|
14 |
// set. For datastores that mixin simpleFetch, simpleFetch
|
|
|
15 |
// implements a fetch method that automatically handles eight of the fetch()
|
|
|
16 |
// arguments -- onBegin, onItem, onComplete, onError, start, count, sort and scope
|
|
|
17 |
// The class mixing in simpleFetch should not implement fetch(),
|
|
|
18 |
// but should instead implement a _fetchItems() method. The _fetchItems()
|
|
|
19 |
// method takes three arguments, the keywordArgs object that was passed
|
|
|
20 |
// to fetch(), a callback function to be called when the result array is
|
|
|
21 |
// available, and an error callback to be called if something goes wrong.
|
|
|
22 |
// The _fetchItems() method should ignore any keywordArgs parameters for
|
|
|
23 |
// start, count, onBegin, onItem, onComplete, onError, sort, and scope.
|
|
|
24 |
// The _fetchItems() method needs to correctly handle any other keywordArgs
|
|
|
25 |
// parameters, including the query parameter and any optional parameters
|
|
|
26 |
// (such as includeChildren). The _fetchItems() method should create an array of
|
|
|
27 |
// result items and pass it to the fetchHandler along with the original request object
|
|
|
28 |
// -- or, the _fetchItems() method may, if it wants to, create an new request object
|
|
|
29 |
// with other specifics about the request that are specific to the datastore and pass
|
|
|
30 |
// that as the request object to the handler.
|
|
|
31 |
//
|
|
|
32 |
// For more information on this specific function, see dojo.data.api.Read.fetch()
|
|
|
33 |
request = request || {};
|
|
|
34 |
if(!request.store){
|
|
|
35 |
request.store = this;
|
|
|
36 |
}
|
|
|
37 |
var self = this;
|
|
|
38 |
|
|
|
39 |
var _errorHandler = function(errorData, requestObject){
|
|
|
40 |
if(requestObject.onError){
|
|
|
41 |
var scope = requestObject.scope || dojo.global;
|
|
|
42 |
requestObject.onError.call(scope, errorData, requestObject);
|
|
|
43 |
}
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
var _fetchHandler = function(items, requestObject){
|
|
|
47 |
var oldAbortFunction = requestObject.abort || null;
|
|
|
48 |
var aborted = false;
|
|
|
49 |
|
|
|
50 |
var startIndex = requestObject.start?requestObject.start:0;
|
|
|
51 |
var endIndex = requestObject.count?(startIndex + requestObject.count):items.length;
|
|
|
52 |
|
|
|
53 |
requestObject.abort = function(){
|
|
|
54 |
aborted = true;
|
|
|
55 |
if(oldAbortFunction){
|
|
|
56 |
oldAbortFunction.call(requestObject);
|
|
|
57 |
}
|
|
|
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 |
|
|
|
90 |
}
|