root / trunk / web / dojo / dojox / data / QueryReadStore.js
History | View | Annotate | Download (5.87 KB)
| 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 |
|
| 8 |
if(!dojo._hasResource["dojox.data.QueryReadStore"]){ |
| 9 |
dojo._hasResource["dojox.data.QueryReadStore"]=true; |
| 10 |
dojo.provide("dojox.data.QueryReadStore");
|
| 11 |
dojo.require("dojo.string");
|
| 12 |
dojo.require("dojo.data.util.sorter");
|
| 13 |
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(_1){ |
| 14 |
dojo.mixin(this,_1);
|
| 15 |
},getValue:function(_2,_3,_4){ |
| 16 |
this._assertIsItem(_2);
|
| 17 |
if(!dojo.isString(_3)){
|
| 18 |
throw new Error(this._className+".getValue(): Invalid attribute, string expected!"); |
| 19 |
} |
| 20 |
if(!this.hasAttribute(_2,_3)){ |
| 21 |
if(_4){
|
| 22 |
return _4;
|
| 23 |
} |
| 24 |
} |
| 25 |
return _2.i[_3];
|
| 26 |
},getValues:function(_5,_6){ |
| 27 |
this._assertIsItem(_5);
|
| 28 |
var _7=[];
|
| 29 |
if(this.hasAttribute(_5,_6)){ |
| 30 |
_7.push(_5.i[_6]); |
| 31 |
} |
| 32 |
return _7;
|
| 33 |
},getAttributes:function(_8){ |
| 34 |
this._assertIsItem(_8);
|
| 35 |
var _9=[];
|
| 36 |
for(var i in _8.i){ |
| 37 |
_9.push(i); |
| 38 |
} |
| 39 |
return _9;
|
| 40 |
},hasAttribute:function(_a,_b){ |
| 41 |
return this.isItem(_a)&&typeof _a.i[_b]!="undefined"; |
| 42 |
},containsValue:function(_c,_d,_e){ |
| 43 |
var _f=this.getValues(_c,_d); |
| 44 |
var len=_f.length;
|
| 45 |
for(var i=0;i<len;i++){ |
| 46 |
if(_f[i]==_e){
|
| 47 |
return true; |
| 48 |
} |
| 49 |
} |
| 50 |
return false; |
| 51 |
},isItem:function(_10){ |
| 52 |
if(_10){
|
| 53 |
return typeof _10.r!="undefined"&&_10.r==this; |
| 54 |
} |
| 55 |
return false; |
| 56 |
},isItemLoaded:function(_11){ |
| 57 |
return this.isItem(_11); |
| 58 |
},loadItem:function(_12){ |
| 59 |
if(this.isItemLoaded(_12.item)){ |
| 60 |
return;
|
| 61 |
} |
| 62 |
},fetch:function(_13){ |
| 63 |
_13=_13||{};
|
| 64 |
if(!_13.store){
|
| 65 |
_13.store=this;
|
| 66 |
} |
| 67 |
var _14=this; |
| 68 |
var _15=function(_16,_17){ |
| 69 |
if(_17.onError){
|
| 70 |
var _18=_17.scope||dojo.global;
|
| 71 |
_17.onError.call(_18,_16,_17); |
| 72 |
} |
| 73 |
}; |
| 74 |
var _19=function(_1a,_1b,_1c){ |
| 75 |
var _1d=_1b.abort||null; |
| 76 |
var _1e=false; |
| 77 |
var _1f=_1b.start?_1b.start:0; |
| 78 |
if(_14.doClientPaging==false){ |
| 79 |
_1f=0;
|
| 80 |
} |
| 81 |
var _20=_1b.count?(_1f+_1b.count):_1a.length;
|
| 82 |
_1b.abort=function(){ |
| 83 |
_1e=true;
|
| 84 |
if(_1d){
|
| 85 |
_1d.call(_1b); |
| 86 |
} |
| 87 |
}; |
| 88 |
var _21=_1b.scope||dojo.global;
|
| 89 |
if(!_1b.store){
|
| 90 |
_1b.store=_14; |
| 91 |
} |
| 92 |
if(_1b.onBegin){
|
| 93 |
_1b.onBegin.call(_21,_1c,_1b); |
| 94 |
} |
| 95 |
if(_1b.sort&&_14.doClientSorting){
|
| 96 |
_1a.sort(dojo.data.util.sorter.createSortFunction(_1b.sort,_14)); |
| 97 |
} |
| 98 |
if(_1b.onItem){
|
| 99 |
for(var i=_1f;(i<_1a.length)&&(i<_20);++i){ |
| 100 |
var _22=_1a[i];
|
| 101 |
if(!_1e){
|
| 102 |
_1b.onItem.call(_21,_22,_1b); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
if(_1b.onComplete&&!_1e){
|
| 107 |
var _23=null; |
| 108 |
if(!_1b.onItem){
|
| 109 |
_23=_1a.slice(_1f,_20); |
| 110 |
} |
| 111 |
_1b.onComplete.call(_21,_23,_1b); |
| 112 |
} |
| 113 |
}; |
| 114 |
this._fetchItems(_13,_19,_15);
|
| 115 |
return _13;
|
| 116 |
},getFeatures:function(){ |
| 117 |
return this._features; |
| 118 |
},close:function(_24){ |
| 119 |
},getLabel:function(_25){ |
| 120 |
if(this._labelAttr&&this.isItem(_25)){ |
| 121 |
return this.getValue(_25,this._labelAttr); |
| 122 |
} |
| 123 |
return undefined; |
| 124 |
},getLabelAttributes:function(_26){ |
| 125 |
if(this._labelAttr){ |
| 126 |
return [this._labelAttr]; |
| 127 |
} |
| 128 |
return null; |
| 129 |
},_xhrFetchHandler:function(_27,_28,_29,_2a){ |
| 130 |
_27=this._filterResponse(_27);
|
| 131 |
if(_27.label){
|
| 132 |
this._labelAttr=_27.label;
|
| 133 |
} |
| 134 |
var _2b=_27.numRows||-1; |
| 135 |
this._items=[];
|
| 136 |
dojo.forEach(_27.items,function(e){
|
| 137 |
this._items.push({i:e,r:this}); |
| 138 |
},this);
|
| 139 |
var _2c=_27.identifier;
|
| 140 |
this._itemsByIdentity={};
|
| 141 |
if(_2c){
|
| 142 |
this._identifier=_2c;
|
| 143 |
var i;
|
| 144 |
for(i=0;i<this._items.length;++i){ |
| 145 |
var _2d=this._items[i].i; |
| 146 |
var _2e=_2d[_2c];
|
| 147 |
if(!this._itemsByIdentity[_2e]){ |
| 148 |
this._itemsByIdentity[_2e]=_2d;
|
| 149 |
}else{
|
| 150 |
throw new Error(this._className+": The json data as specified by: ["+this.url+"] is malformed. Items within the list have identifier: ["+_2c+"]. Value collided: ["+_2e+"]"); |
| 151 |
} |
| 152 |
} |
| 153 |
}else{
|
| 154 |
this._identifier=Number;
|
| 155 |
for(i=0;i<this._items.length;++i){ |
| 156 |
this._items[i].n=i;
|
| 157 |
} |
| 158 |
} |
| 159 |
_2b=this._numRows=(_2b===-1)?this._items.length:_2b; |
| 160 |
_29(this._items,_28,_2b);
|
| 161 |
this._numRows=_2b;
|
| 162 |
},_fetchItems:function(_2f,_30,_31){ |
| 163 |
var _32=_2f.serverQuery||_2f.query||{};
|
| 164 |
if(!this.doClientPaging){ |
| 165 |
_32.start=_2f.start||0;
|
| 166 |
if(_2f.count){
|
| 167 |
_32.count=_2f.count; |
| 168 |
} |
| 169 |
} |
| 170 |
if(!this.doClientSorting&&_2f.sort){ |
| 171 |
var _33=[];
|
| 172 |
dojo.forEach(_2f.sort,function(_34){
|
| 173 |
if(_34&&_34.attribute){
|
| 174 |
_33.push((_34.descending?"-":"")+_34.attribute); |
| 175 |
} |
| 176 |
}); |
| 177 |
_32.sort=_33.join(",");
|
| 178 |
} |
| 179 |
if(this.doClientPaging&&this._lastServerQuery!==null&&dojo.toJson(_32)==dojo.toJson(this._lastServerQuery)){ |
| 180 |
this._numRows=(this._numRows===-1)?this._items.length:this._numRows; |
| 181 |
_30(this._items,_2f,this._numRows); |
| 182 |
}else{
|
| 183 |
var _35=this.requestMethod.toLowerCase()=="post"?dojo.xhrPost:dojo.xhrGet; |
| 184 |
var _36=_35({url:this.url,handleAs:"json-comment-optional",content:_32}); |
| 185 |
_36.addCallback(dojo.hitch(this,function(_37){ |
| 186 |
this._xhrFetchHandler(_37,_2f,_30,_31);
|
| 187 |
})); |
| 188 |
_36.addErrback(function(_38){
|
| 189 |
_31(_38,_2f); |
| 190 |
}); |
| 191 |
this.lastRequestHash=new Date().getTime()+"-"+String(Math.random()).substring(2); |
| 192 |
this._lastServerQuery=dojo.mixin({},_32);
|
| 193 |
} |
| 194 |
},_filterResponse:function(_39){ |
| 195 |
return _39;
|
| 196 |
},_assertIsItem:function(_3a){ |
| 197 |
if(!this.isItem(_3a)){ |
| 198 |
throw new Error(this._className+": Invalid item argument."); |
| 199 |
} |
| 200 |
},_assertIsAttribute:function(_3b){ |
| 201 |
if(typeof _3b!=="string"){ |
| 202 |
throw new Error(this._className+": Invalid attribute argument ('"+_3b+"')."); |
| 203 |
} |
| 204 |
},fetchItemByIdentity:function(_3c){ |
| 205 |
if(this._itemsByIdentity){ |
| 206 |
var _3d=this._itemsByIdentity[_3c.identity]; |
| 207 |
if(!(_3d===undefined)){ |
| 208 |
if(_3c.onItem){
|
| 209 |
var _3e=_3c.scope?_3c.scope:dojo.global;
|
| 210 |
_3c.onItem.call(_3e,{i:_3d,r:this});
|
| 211 |
} |
| 212 |
return;
|
| 213 |
} |
| 214 |
} |
| 215 |
var _3f=function(_40,_41){ |
| 216 |
var _42=_3c.scope?_3c.scope:dojo.global;
|
| 217 |
if(_3c.onError){
|
| 218 |
_3c.onError.call(_42,_40); |
| 219 |
} |
| 220 |
}; |
| 221 |
var _43=function(_44,_45){ |
| 222 |
var _46=_3c.scope?_3c.scope:dojo.global;
|
| 223 |
try{
|
| 224 |
var _47=null; |
| 225 |
if(_44&&_44.length==1){ |
| 226 |
_47=_44[0];
|
| 227 |
} |
| 228 |
if(_3c.onItem){
|
| 229 |
_3c.onItem.call(_46,_47); |
| 230 |
} |
| 231 |
} |
| 232 |
catch(error){
|
| 233 |
if(_3c.onError){
|
| 234 |
_3c.onError.call(_46,error); |
| 235 |
} |
| 236 |
} |
| 237 |
}; |
| 238 |
var _48={serverQuery:{id:_3c.identity}}; |
| 239 |
this._fetchItems(_48,_43,_3f);
|
| 240 |
},getIdentity:function(_49){ |
| 241 |
var _4a=null; |
| 242 |
if(this._identifier===Number){ |
| 243 |
_4a=_49.n; |
| 244 |
}else{
|
| 245 |
_4a=_49.i[this._identifier];
|
| 246 |
} |
| 247 |
return _4a;
|
| 248 |
},getIdentityAttributes:function(_4b){ |
| 249 |
return [this._identifier]; |
| 250 |
}}); |
| 251 |
} |