123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- define("dojox/data/KeyValueStore", ["dojo/_base/declare", "dojo/_base/lang", "dojo/_base/xhr", "dojo/_base/window",
- "dojo/data/util/simpleFetch", "dojo/data/util/filter"],
- function(declare, lang, xhr, winUtil, simpleFetch, filterUtil) {
- var KeyValueStore = declare("dojox.data.KeyValueStore", null, {
-
-
-
-
-
-
-
-
-
-
-
- constructor: function(/* Object */ keywordParameters){
-
-
-
-
- if(keywordParameters.url){
- this.url = keywordParameters.url;
- }
- this._keyValueString = keywordParameters.data;
- this._keyValueVar = keywordParameters.dataVar;
- this._keyAttribute = "key";
- this._valueAttribute = "value";
- this._storeProp = "_keyValueStore";
- this._features = {
- 'dojo.data.api.Read': true,
- 'dojo.data.api.Identity': true
- };
- this._loadInProgress = false;
- this._queuedFetches = [];
- if(keywordParameters && "urlPreventCache" in keywordParameters){
- this.urlPreventCache = keywordParameters.urlPreventCache?true:false;
- }
- },
-
- url: "",
- data: "",
-
-
- urlPreventCache: false,
-
- _assertIsItem: function(/* item */ item){
-
-
-
-
- if(!this.isItem(item)){
- throw new Error("dojox.data.KeyValueStore: a function was passed an item argument that was not an item");
- }
- },
-
- _assertIsAttribute: function(/* item */ item, /* String */ attribute){
-
-
-
-
- if(!lang.isString(attribute)){
- throw new Error("dojox.data.KeyValueStore: a function was passed an attribute argument that was not an attribute object nor an attribute name string");
- }
- },
- getValue: function( /* item */ item,
- /* attribute-name-string */ attribute,
- /* value? */ defaultValue){
-
-
- this._assertIsItem(item);
- this._assertIsAttribute(item, attribute);
- var value;
- if(attribute == this._keyAttribute){
- value = item[this._keyAttribute];
- }else{
- value = item[this._valueAttribute];
- }
- if(value === undefined){
- value = defaultValue;
- }
- return value;
- },
- getValues: function(/* item */ item,
- /* attribute-name-string */ attribute){
-
-
-
-
- var value = this.getValue(item, attribute);
- return (value ? [value] : []);
- },
- getAttributes: function(/* item */ item){
-
-
- return [this._keyAttribute, this._valueAttribute, item[this._keyAttribute]];
- },
- hasAttribute: function( /* item */ item,
- /* attribute-name-string */ attribute){
-
-
- this._assertIsItem(item);
- this._assertIsAttribute(item, attribute);
- return (attribute == this._keyAttribute || attribute == this._valueAttribute || attribute == item[this._keyAttribute]);
- },
- containsValue: function(/* item */ item,
- /* attribute-name-string */ attribute,
- /* anything */ value){
-
-
- var regexp = undefined;
- if(typeof value === "string"){
- regexp = filterUtil.patternToRegExp(value, false);
- }
- return this._containsValue(item, attribute, value, regexp);
- },
- _containsValue: function( /* item */ item,
- /* attribute || attribute-name-string */ attribute,
- /* anything */ value,
- /* RegExp?*/ regexp){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var values = this.getValues(item, attribute);
- for(var i = 0; i < values.length; ++i){
- var possibleValue = values[i];
- if(typeof possibleValue === "string" && regexp){
- return (possibleValue.match(regexp) !== null);
- }else{
-
- if(value === possibleValue){
- return true;
- }
- }
- }
- return false;
- },
- isItem: function(/* anything */ something){
-
-
- if(something && something[this._storeProp] === this){
- return true;
- }
- return false;
- },
- isItemLoaded: function(/* anything */ something){
-
-
-
- return this.isItem(something);
- },
- loadItem: function(/* object */ keywordArgs){
-
-
-
-
-
-
-
-
- },
- getFeatures: function(){
-
-
- return this._features;
- },
- close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
-
-
- },
- getLabel: function(/* item */ item){
-
-
- return item[this._keyAttribute];
- },
- getLabelAttributes: function(/* item */ item){
-
-
- return [this._keyAttribute];
- },
-
-
-
-
- _fetchItems: function( /* Object */ keywordArgs,
- /* Function */ findCallback,
- /* Function */ errorCallback){
-
-
-
- var self = this;
- var filter = function(requestArgs, arrayOfAllItems){
- var items = null;
- if(requestArgs.query){
- items = [];
- var ignoreCase = requestArgs.queryOptions ? requestArgs.queryOptions.ignoreCase : false;
-
-
- var regexpList = {};
- for(var key in requestArgs.query){
- var value = requestArgs.query[key];
- if(typeof value === "string"){
- regexpList[key] = filterUtil.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{
-
-
- 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(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 = xhr.get(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.");
- }
- }
-
- },
- _handleQueuedFetches: function(){
-
-
-
- if(this._queuedFetches.length > 0){
- for(var i = 0; i < this._queuedFetches.length; i++){
- var fData = this._queuedFetches[i];
- var delayedFilter = fData.filter;
- var delayedQuery = fData.args;
- if(delayedFilter){
- delayedFilter(delayedQuery, this._arrayOfAllItems);
- }else{
- this.fetchItemByIdentity(fData.args);
- }
- }
- this._queuedFetches = [];
- }
- },
-
- _processData: function(/* Array */ data){
- this._arrayOfAllItems = [];
- for(var i=0; i<data.length; i++){
- this._arrayOfAllItems.push(this._createItem(data[i]));
- }
- this._loadFinished = true;
- this._loadInProgress = false;
- },
-
- _createItem: function(/* Object */ something){
- var item = {};
- item[this._storeProp] = this;
- for(var i in something){
- item[this._keyAttribute] = i;
- item[this._valueAttribute] = something[i];
- break;
- }
- return item;
- },
- getIdentity: function(/* item */ item){
-
-
- if(this.isItem(item)){
- return item[this._keyAttribute];
- }
- return null;
- },
- getIdentityAttributes: function(/* item */ item){
-
-
- return [this._keyAttribute];
- },
- fetchItemByIdentity: function(/* object */ keywordArgs){
-
-
- keywordArgs.oldOnItem = keywordArgs.onItem;
- keywordArgs.onItem = null;
- keywordArgs.onComplete = this._finishFetchItemByIdentity ;
- this.fetch(keywordArgs);
- },
-
- _finishFetchItemByIdentity: function(/* Array */ items, /* object */ request){
- var scope = request.scope || winUtil.global;
- if(items.length){
- request.oldOnItem.call(scope, items[0]);
- }else{
- request.oldOnItem.call(scope, null);
- }
- }
- });
- lang.extend(KeyValueStore,simpleFetch);
- return KeyValueStore;
- });
|