123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- if(!dojo._hasResource["dojox.data.QueryReadStore"]){
- dojo._hasResource["dojox.data.QueryReadStore"] = true;
- dojo.provide("dojox.data.QueryReadStore");
- dojo.require("dojo.data.util.sorter");
- dojo.require("dojo.string");
- dojo.declare("dojox.data.QueryReadStore",
- null,
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- url:"",
- requestMethod:"get",
-
-
-
- _className:"dojox.data.QueryReadStore",
-
-
-
-
-
-
-
-
- _items:[],
-
-
-
-
- _lastServerQuery:null,
-
-
- _numRows:-1,
-
-
-
-
- lastRequestHash:null,
-
-
-
- doClientPaging:false,
-
-
-
-
- doClientSorting:false,
-
-
- _itemsByIdentity:null,
-
-
- _identifier:null,
-
- _features: {'dojo.data.api.Read':true, 'dojo.data.api.Identity':true},
-
- _labelAttr: "label",
-
- constructor: function(/* Object */ params){
- dojo.mixin(this,params);
- },
-
- getValue: function(/* item */ item, /* attribute-name-string */ attribute, /* value? */ defaultValue){
-
-
- this._assertIsItem(item);
- if(!dojo.isString(attribute)){
- throw new Error(this._className+".getValue(): Invalid attribute, string expected!");
- }
- if(!this.hasAttribute(item, attribute)){
-
-
- if(defaultValue){
- return defaultValue;
- }
- }
- return item.i[attribute];
- },
-
- getValues: function(/* item */ item, /* attribute-name-string */ attribute){
- this._assertIsItem(item);
- var ret = [];
- if(this.hasAttribute(item, attribute)){
- ret.push(item.i[attribute]);
- }
- return ret;
- },
-
- getAttributes: function(/* item */ item){
- this._assertIsItem(item);
- var ret = [];
- for(var i in item.i){
- ret.push(i);
- }
- return ret;
- },
-
- hasAttribute: function(/* item */ item, /* attribute-name-string */ attribute){
-
-
- return this.isItem(item) && typeof item.i[attribute]!="undefined";
- },
-
- containsValue: function(/* item */ item, /* attribute-name-string */ attribute, /* anything */ value){
- var values = this.getValues(item, attribute);
- var len = values.length;
- for(var i=0; i<len; i++){
- if(values[i] == value){
- return true;
- }
- }
- return false;
- },
-
- isItem: function(/* anything */ something){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(something){
- return typeof something.r != "undefined" && something.r == this;
- }
- return false;
- },
-
- isItemLoaded: function(/* anything */ something){
-
-
-
- return this.isItem(something);
- },
-
- loadItem: function(/* object */ args){
- if(this.isItemLoaded(args.item)){
- return;
- }
-
- },
-
- fetch:function(/* Object? */ request){
-
-
-
-
-
-
- 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, numRows){
- var oldAbortFunction = requestObject.abort || null;
- var aborted = false;
-
- var startIndex = requestObject.start?requestObject.start:0;
- if(self.doClientPaging == false){
-
- startIndex = 0;
- }
- var endIndex = requestObject.count?(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, numRows, requestObject);
- }
- if(requestObject.sort && self.doClientSorting){
- 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;
- },
-
- getFeatures: function(){
- return this._features;
- },
-
- close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
-
- },
-
- getLabel: function(/* item */ item){
-
-
- if(this._labelAttr && this.isItem(item)){
- return this.getValue(item, this._labelAttr);
- }
- return undefined;
- },
-
- getLabelAttributes: function(/* item */ item){
-
-
- if(this._labelAttr){
- return [this._labelAttr];
- }
- return null;
- },
-
- _xhrFetchHandler: function(data, request, fetchHandler, errorHandler){
- data = this._filterResponse(data);
- if(data.label){
- this._labelAttr = data.label;
- }
- var numRows = data.numRows || -1;
- this._items = [];
-
-
-
- dojo.forEach(data.items,function(e){
- this._items.push({i:e, r:this});
- },this);
-
- var identifier = data.identifier;
- this._itemsByIdentity = {};
- if(identifier){
- this._identifier = identifier;
- var i;
- for(i = 0; i < this._items.length; ++i){
- var item = this._items[i].i;
- var identity = item[identifier];
- if(!this._itemsByIdentity[identity]){
- this._itemsByIdentity[identity] = item;
- }else{
- throw new Error(this._className+": The json data as specified by: [" + this.url + "] is malformed. Items within the list have identifier: [" + identifier + "]. Value collided: [" + identity + "]");
- }
- }
- }else{
- this._identifier = Number;
- for(i = 0; i < this._items.length; ++i){
- this._items[i].n = i;
- }
- }
-
-
-
- numRows = this._numRows = (numRows === -1) ? this._items.length : numRows;
- fetchHandler(this._items, request, numRows);
- this._numRows = numRows;
- },
-
- _fetchItems: function(request, fetchHandler, errorHandler){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var serverQuery = request.serverQuery || request.query || {};
-
- if(!this.doClientPaging){
- serverQuery.start = request.start || 0;
-
- if(request.count){
- serverQuery.count = request.count;
- }
- }
- if(!this.doClientSorting && request.sort){
- var sortInfo = [];
- dojo.forEach(request.sort, function(sort){
- if(sort && sort.attribute){
- sortInfo.push((sort.descending ? "-" : "") + sort.attribute);
- }
- });
- serverQuery.sort = sortInfo.join(',');
- }
-
-
- if(this.doClientPaging && this._lastServerQuery !== null &&
- dojo.toJson(serverQuery) == dojo.toJson(this._lastServerQuery)
- ){
- this._numRows = (this._numRows === -1) ? this._items.length : this._numRows;
- fetchHandler(this._items, request, this._numRows);
- }else{
- var xhrFunc = this.requestMethod.toLowerCase() == "post" ? dojo.xhrPost : dojo.xhrGet;
- var xhrHandler = xhrFunc({url:this.url, handleAs:"json-comment-optional", content:serverQuery, failOk: true});
- request.abort = function(){
- xhrHandler.cancel();
- };
- xhrHandler.addCallback(dojo.hitch(this, function(data){
- this._xhrFetchHandler(data, request, fetchHandler, errorHandler);
- }));
- xhrHandler.addErrback(function(error){
- errorHandler(error, request);
- });
-
-
-
- this.lastRequestHash = new Date().getTime()+"-"+String(Math.random()).substring(2);
- this._lastServerQuery = dojo.mixin({}, serverQuery);
- }
- },
-
- _filterResponse: function(data){
-
-
-
-
-
-
- return data;
- },
-
- _assertIsItem: function(/* item */ item){
-
-
-
-
-
- if(!this.isItem(item)){
- throw new Error(this._className+": Invalid item argument.");
- }
- },
-
- _assertIsAttribute: function(/* attribute-name-string */ attribute){
-
-
-
-
- if(typeof attribute !== "string"){
- throw new Error(this._className+": Invalid attribute argument ('"+attribute+"').");
- }
- },
-
- fetchItemByIdentity: function(/* Object */ keywordArgs){
-
-
-
-
-
-
- if(this._itemsByIdentity){
- var item = this._itemsByIdentity[keywordArgs.identity];
- if(!(item === undefined)){
- if(keywordArgs.onItem){
- var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
- keywordArgs.onItem.call(scope, {i:item, r:this});
- }
- return;
- }
- }
-
-
-
- var _errorHandler = function(errorData, requestObject){
- var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
- if(keywordArgs.onError){
- keywordArgs.onError.call(scope, errorData);
- }
- };
-
-
- var _fetchHandler = function(items, requestObject){
- var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
- try{
-
- var item = null;
- if(items && items.length == 1){
- item = items[0];
- }
-
-
-
- if(keywordArgs.onItem){
- keywordArgs.onItem.call(scope, item);
- }
- }catch(error){
- if(keywordArgs.onError){
- keywordArgs.onError.call(scope, error);
- }
- }
- };
-
-
- var request = {serverQuery:{id:keywordArgs.identity}};
-
-
- this._fetchItems(request, _fetchHandler, _errorHandler);
- },
-
- getIdentity: function(/* item */ item){
-
-
- var identifier = null;
- if(this._identifier === Number){
- identifier = item.n;
- }else{
- identifier = item.i[this._identifier];
- }
- return identifier;
- },
-
- getIdentityAttributes: function(/* item */ item){
-
-
- return [this._identifier];
- }
- }
- );
- }
|