1 | define(["dojo/_base/lang", "dojo/_base/window", "./sorter"], |
---|
2 | function(lang, winUtil, sorter) { |
---|
3 | // module: |
---|
4 | // dojo/data/util/simpleFetch |
---|
5 | // summary: |
---|
6 | // TODOC |
---|
7 | |
---|
8 | var simpleFetch = lang.getObject("dojo.data.util.simpleFetch", true); |
---|
9 | |
---|
10 | simpleFetch.fetch = function(/* Object? */ request){ |
---|
11 | // summary: |
---|
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. |
---|
35 | // |
---|
36 | // For more information on this specific function, see dojo.data.api.Read.fetch() |
---|
37 | request = request || {}; |
---|
38 | if(!request.store){ |
---|
39 | request.store = this; |
---|
40 | } |
---|
41 | var self = this; |
---|
42 | |
---|
43 | var _errorHandler = function(errorData, requestObject){ |
---|
44 | if(requestObject.onError){ |
---|
45 | var scope = requestObject.scope || winUtil.global; |
---|
46 | requestObject.onError.call(scope, errorData, requestObject); |
---|
47 | } |
---|
48 | }; |
---|
49 | |
---|
50 | var _fetchHandler = function(items, requestObject){ |
---|
51 | var oldAbortFunction = requestObject.abort || null; |
---|
52 | var aborted = false; |
---|
53 | |
---|
54 | var startIndex = requestObject.start?requestObject.start:0; |
---|
55 | var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length; |
---|
56 | |
---|
57 | requestObject.abort = function(){ |
---|
58 | aborted = true; |
---|
59 | if(oldAbortFunction){ |
---|
60 | oldAbortFunction.call(requestObject); |
---|
61 | } |
---|
62 | }; |
---|
63 | |
---|
64 | var scope = requestObject.scope || winUtil.global; |
---|
65 | if(!requestObject.store){ |
---|
66 | requestObject.store = self; |
---|
67 | } |
---|
68 | if(requestObject.onBegin){ |
---|
69 | requestObject.onBegin.call(scope, items.length, requestObject); |
---|
70 | } |
---|
71 | if(requestObject.sort){ |
---|
72 | items.sort(sorter.createSortFunction(requestObject.sort, self)); |
---|
73 | } |
---|
74 | if(requestObject.onItem){ |
---|
75 | for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){ |
---|
76 | var item = items[i]; |
---|
77 | if(!aborted){ |
---|
78 | requestObject.onItem.call(scope, item, requestObject); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | if(requestObject.onComplete && !aborted){ |
---|
83 | var subset = null; |
---|
84 | if(!requestObject.onItem){ |
---|
85 | subset = items.slice(startIndex, endIndex); |
---|
86 | } |
---|
87 | requestObject.onComplete.call(scope, subset, requestObject); |
---|
88 | } |
---|
89 | }; |
---|
90 | this._fetchItems(request, _fetchHandler, _errorHandler); |
---|
91 | return request; // Object |
---|
92 | }; |
---|
93 | |
---|
94 | return simpleFetch; |
---|
95 | }); |
---|