2010-11-15 08:39:52 +01:00
|
|
|
/*
|
2011-11-08 17:40:44 +01:00
|
|
|
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
|
2010-11-15 08:39:52 +01:00
|
|
|
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
|
|
|
see: http://dojotoolkit.org/license for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
if(!dojo._hasResource["dojo.data.util.simpleFetch"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
|
dojo._hasResource["dojo.data.util.simpleFetch"] = true;
|
2010-11-15 08:39:52 +01:00
|
|
|
dojo.provide("dojo.data.util.simpleFetch");
|
|
|
|
dojo.require("dojo.data.util.sorter");
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2011-11-08 17:40:44 +01:00
|
|
|
dojo.getObject("data.util.simpleFetch", true, dojo);
|
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
dojo.data.util.simpleFetch.fetch = function(/* Object? */ request){
|
|
|
|
// summary:
|
|
|
|
// The simpleFetch mixin is designed to serve as a set of function(s) that can
|
2011-11-08 17:40:44 +01:00
|
|
|
// be mixed into other datastore implementations to accelerate their development.
|
|
|
|
// The simpleFetch mixin should work well for any datastore that can respond to a _fetchItems()
|
2011-03-04 17:02:28 +01:00
|
|
|
// call by returning an array of all the found items that matched the query. The simpleFetch mixin
|
|
|
|
// is not designed to work for datastores that respond to a fetch() call by incrementally
|
|
|
|
// loading items, or sequentially loading partial batches of the result
|
2011-11-08 17:40:44 +01:00
|
|
|
// set. For datastores that mixin simpleFetch, simpleFetch
|
2011-03-04 17:02:28 +01:00
|
|
|
// implements a fetch method that automatically handles eight of the fetch()
|
|
|
|
// arguments -- onBegin, onItem, onComplete, onError, start, count, sort and scope
|
|
|
|
// The class mixing in simpleFetch should not implement fetch(),
|
2011-11-08 17:40:44 +01:00
|
|
|
// but should instead implement a _fetchItems() method. The _fetchItems()
|
|
|
|
// method takes three arguments, the keywordArgs object that was passed
|
2011-03-04 17:02:28 +01:00
|
|
|
// to fetch(), a callback function to be called when the result array is
|
|
|
|
// available, and an error callback to be called if something goes wrong.
|
|
|
|
// The _fetchItems() method should ignore any keywordArgs parameters for
|
2011-11-08 17:40:44 +01:00
|
|
|
// start, count, onBegin, onItem, onComplete, onError, sort, and scope.
|
2011-03-04 17:02:28 +01:00
|
|
|
// The _fetchItems() method needs to correctly handle any other keywordArgs
|
2011-11-08 17:40:44 +01:00
|
|
|
// parameters, including the query parameter and any optional parameters
|
|
|
|
// (such as includeChildren). The _fetchItems() method should create an array of
|
|
|
|
// result items and pass it to the fetchHandler along with the original request object
|
|
|
|
// -- or, the _fetchItems() method may, if it wants to, create an new request object
|
|
|
|
// with other specifics about the request that are specific to the datastore and pass
|
2011-03-04 17:02:28 +01:00
|
|
|
// that as the request object to the handler.
|
|
|
|
//
|
|
|
|
// For more information on this specific function, see dojo.data.api.Read.fetch()
|
|
|
|
request = request || {};
|
|
|
|
if(!request.store){
|
|
|
|
request.store = this;
|
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var _errorHandler = function(errorData, requestObject){
|
|
|
|
if(requestObject.onError){
|
|
|
|
var scope = requestObject.scope || dojo.global;
|
|
|
|
requestObject.onError.call(scope, errorData, requestObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var _fetchHandler = function(items, requestObject){
|
|
|
|
var oldAbortFunction = requestObject.abort || null;
|
|
|
|
var aborted = false;
|
|
|
|
|
|
|
|
var startIndex = requestObject.start?requestObject.start:0;
|
|
|
|
var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length;
|
|
|
|
|
|
|
|
requestObject.abort = function(){
|
|
|
|
aborted = true;
|
|
|
|
if(oldAbortFunction){
|
|
|
|
oldAbortFunction.call(requestObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var scope = requestObject.scope || dojo.global;
|
|
|
|
if(!requestObject.store){
|
|
|
|
requestObject.store = self;
|
|
|
|
}
|
|
|
|
if(requestObject.onBegin){
|
|
|
|
requestObject.onBegin.call(scope, items.length, requestObject);
|
|
|
|
}
|
|
|
|
if(requestObject.sort){
|
|
|
|
items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self));
|
|
|
|
}
|
|
|
|
if(requestObject.onItem){
|
|
|
|
for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){
|
|
|
|
var item = items[i];
|
|
|
|
if(!aborted){
|
|
|
|
requestObject.onItem.call(scope, item, requestObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(requestObject.onComplete && !aborted){
|
|
|
|
var subset = null;
|
|
|
|
if(!requestObject.onItem){
|
|
|
|
subset = items.slice(startIndex, endIndex);
|
|
|
|
}
|
|
|
|
requestObject.onComplete.call(scope, subset, requestObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._fetchItems(request, _fetchHandler, _errorHandler);
|
|
|
|
return request; // Object
|
2010-11-15 08:39:52 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2010-11-15 08:39:52 +01:00
|
|
|
}
|