]>
Commit | Line | Data |
---|---|---|
2f01fe57 AD |
1 | /* |
2 | Copyright (c) 2004-2010, 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 | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
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; | |
2f01fe57 AD |
10 | dojo.provide("dojo.data.util.simpleFetch"); |
11 | dojo.require("dojo.data.util.sorter"); | |
a089699c AD |
12 | |
13 | dojo.data.util.simpleFetch.fetch = function(/* Object? */ request){ | |
14 | // summary: | |
15 | // The simpleFetch mixin is designed to serve as a set of function(s) that can | |
16 | // be mixed into other datastore implementations to accelerate their development. | |
17 | // The simpleFetch mixin should work well for any datastore that can respond to a _fetchItems() | |
18 | // call by returning an array of all the found items that matched the query. The simpleFetch mixin | |
19 | // is not designed to work for datastores that respond to a fetch() call by incrementally | |
20 | // loading items, or sequentially loading partial batches of the result | |
21 | // set. For datastores that mixin simpleFetch, simpleFetch | |
22 | // implements a fetch method that automatically handles eight of the fetch() | |
23 | // arguments -- onBegin, onItem, onComplete, onError, start, count, sort and scope | |
24 | // The class mixing in simpleFetch should not implement fetch(), | |
25 | // but should instead implement a _fetchItems() method. The _fetchItems() | |
26 | // method takes three arguments, the keywordArgs object that was passed | |
27 | // to fetch(), a callback function to be called when the result array is | |
28 | // available, and an error callback to be called if something goes wrong. | |
29 | // The _fetchItems() method should ignore any keywordArgs parameters for | |
30 | // start, count, onBegin, onItem, onComplete, onError, sort, and scope. | |
31 | // The _fetchItems() method needs to correctly handle any other keywordArgs | |
32 | // parameters, including the query parameter and any optional parameters | |
33 | // (such as includeChildren). The _fetchItems() method should create an array of | |
34 | // result items and pass it to the fetchHandler along with the original request object | |
35 | // -- or, the _fetchItems() method may, if it wants to, create an new request object | |
36 | // with other specifics about the request that are specific to the datastore and pass | |
37 | // that as the request object to the handler. | |
38 | // | |
39 | // For more information on this specific function, see dojo.data.api.Read.fetch() | |
40 | request = request || {}; | |
41 | if(!request.store){ | |
42 | request.store = this; | |
43 | } | |
44 | var self = this; | |
45 | ||
46 | var _errorHandler = function(errorData, requestObject){ | |
47 | if(requestObject.onError){ | |
48 | var scope = requestObject.scope || dojo.global; | |
49 | requestObject.onError.call(scope, errorData, requestObject); | |
50 | } | |
51 | }; | |
52 | ||
53 | var _fetchHandler = function(items, requestObject){ | |
54 | var oldAbortFunction = requestObject.abort || null; | |
55 | var aborted = false; | |
56 | ||
57 | var startIndex = requestObject.start?requestObject.start:0; | |
58 | var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length; | |
59 | ||
60 | requestObject.abort = function(){ | |
61 | aborted = true; | |
62 | if(oldAbortFunction){ | |
63 | oldAbortFunction.call(requestObject); | |
64 | } | |
65 | }; | |
66 | ||
67 | var scope = requestObject.scope || dojo.global; | |
68 | if(!requestObject.store){ | |
69 | requestObject.store = self; | |
70 | } | |
71 | if(requestObject.onBegin){ | |
72 | requestObject.onBegin.call(scope, items.length, requestObject); | |
73 | } | |
74 | if(requestObject.sort){ | |
75 | items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self)); | |
76 | } | |
77 | if(requestObject.onItem){ | |
78 | for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){ | |
79 | var item = items[i]; | |
80 | if(!aborted){ | |
81 | requestObject.onItem.call(scope, item, requestObject); | |
82 | } | |
83 | } | |
84 | } | |
85 | if(requestObject.onComplete && !aborted){ | |
86 | var subset = null; | |
87 | if(!requestObject.onItem){ | |
88 | subset = items.slice(startIndex, endIndex); | |
89 | } | |
90 | requestObject.onComplete.call(scope, subset, requestObject); | |
91 | } | |
92 | }; | |
93 | this._fetchItems(request, _fetchHandler, _errorHandler); | |
94 | return request; // Object | |
2f01fe57 | 95 | }; |
a089699c | 96 | |
2f01fe57 | 97 | } |