source: [view]
var self = this;
var filter = function(requestArgs, arrayOfAllItems){
var items = null;
if(requestArgs.query){
items = [];
var ignoreCase = requestArgs.queryOptions ? requestArgs.queryOptions.ignoreCase : false;
//See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the
//same value for each item examined. Much more efficient.
var regexpList = {};
for(var key in requestArgs.query){
var value = requestArgs.query[key];
if(typeof value === "string"){
regexpList[key] = dojo.data.util.filter.patternToRegExp(value, ignoreCase);
}
}
for(var i = 0; i < arrayOfAllItems.length; ++i){
var match = true;
var candidateItem = arrayOfAllItems[i];
for(var key in requestArgs.query){
var value = requestArgs.query[key];
if(!self._containsValue(candidateItem, key, value, regexpList[key])){
match = false;
}
}
if(match){
items.push(candidateItem);
}
}
}else if(requestArgs.identity){
items = [];
var item;
for(var key in arrayOfAllItems){
item = arrayOfAllItems[key];
if(item[self._keyAttribute] == requestArgs.identity){
items.push(item);
break;
}
}
}else{
// We want a copy to pass back in case the parent wishes to sort the array. We shouldn't allow resort
// of the internal list so that multiple callers can get lists and sort without affecting each other.
if(arrayOfAllItems.length> 0){
items = arrayOfAllItems.slice(0,arrayOfAllItems.length);
}
}
findCallback(items, requestArgs);
};
if(this._loadFinished){
filter(keywordArgs, this._arrayOfAllItems);
}else{
if(this.url !== ""){
//If fetches come in before the loading has finished, but while
//a load is in progress, we have to defer the fetching to be
//invoked in the callback.
if(this._loadInProgress){
this._queuedFetches.push({args: keywordArgs, filter: filter});
}else{
this._loadInProgress = true;
var getArgs = {
url: self.url,
handleAs: "json-comment-filtered",
preventCache: this.urlPreventCache
};
var getHandler = dojo.xhrGet(getArgs);
getHandler.addCallback(function(data){
self._processData(data);
filter(keywordArgs, self._arrayOfAllItems);
self._handleQueuedFetches();
});
getHandler.addErrback(function(error){
self._loadInProgress = false;
throw error;
});
}
}else if(this._keyValueString){
this._processData(eval(this._keyValueString));
this._keyValueString = null;
filter(keywordArgs, this._arrayOfAllItems);
}else if(this._keyValueVar){
this._processData(this._keyValueVar);
this._keyValueVar = null;
filter(keywordArgs, this._arrayOfAllItems);
}else{
throw new Error("dojox.data.KeyValueStore: No source data was provided as either URL, String, or Javascript variable data input.");
}
}