root / trunk / web / dojo / dojox / data / KeyValueStore.js @ 12
History | View | Annotate | Download (5.43 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
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.KeyValueStore"]){ |
||
9 | dojo._hasResource["dojox.data.KeyValueStore"]=true; |
||
10 | dojo.provide("dojox.data.KeyValueStore");
|
||
11 | dojo.require("dojo.data.util.filter");
|
||
12 | dojo.require("dojo.data.util.simpleFetch");
|
||
13 | dojo.declare("dojox.data.KeyValueStore",null,{constructor:function(_1){ |
||
14 | if(_1.url){
|
||
15 | this.url=_1.url;
|
||
16 | } |
||
17 | this._keyValueString=_1.data;
|
||
18 | this._keyValueVar=_1.dataVar;
|
||
19 | this._keyAttribute="key"; |
||
20 | this._valueAttribute="value"; |
||
21 | this._storeProp="_keyValueStore"; |
||
22 | this._features={"dojo.data.api.Read":true,"dojo.data.api.Identity":true}; |
||
23 | this._loadInProgress=false; |
||
24 | this._queuedFetches=[];
|
||
25 | if(_1&&"urlPreventCache" in _1){ |
||
26 | this.urlPreventCache=_1.urlPreventCache?true:false; |
||
27 | } |
||
28 | },url:"",data:"",urlPreventCache:false,_assertIsItem:function(_2){ |
||
29 | if(!this.isItem(_2)){ |
||
30 | throw new Error("dojox.data.KeyValueStore: a function was passed an item argument that was not an item"); |
||
31 | } |
||
32 | },_assertIsAttribute:function(_3,_4){ |
||
33 | if(!dojo.isString(_4)){
|
||
34 | throw new Error("dojox.data.KeyValueStore: a function was passed an attribute argument that was not an attribute object nor an attribute name string"); |
||
35 | } |
||
36 | },getValue:function(_5,_6,_7){ |
||
37 | this._assertIsItem(_5);
|
||
38 | this._assertIsAttribute(_5,_6);
|
||
39 | var _8;
|
||
40 | if(_6==this._keyAttribute){ |
||
41 | _8=_5[this._keyAttribute];
|
||
42 | }else{
|
||
43 | _8=_5[this._valueAttribute];
|
||
44 | } |
||
45 | if(_8===undefined){ |
||
46 | _8=_7; |
||
47 | } |
||
48 | return _8;
|
||
49 | },getValues:function(_9,_a){ |
||
50 | var _b=this.getValue(_9,_a); |
||
51 | return (_b?[_b]:[]);
|
||
52 | },getAttributes:function(_c){ |
||
53 | return [this._keyAttribute,this._valueAttribute,_c[this._keyAttribute]]; |
||
54 | },hasAttribute:function(_d,_e){ |
||
55 | this._assertIsItem(_d);
|
||
56 | this._assertIsAttribute(_d,_e);
|
||
57 | return (_e==this._keyAttribute||_e==this._valueAttribute||_e==_d[this._keyAttribute]); |
||
58 | },containsValue:function(_f,_10,_11){ |
||
59 | var _12=undefined; |
||
60 | if(typeof _11==="string"){ |
||
61 | _12=dojo.data.util.filter.patternToRegExp(_11,false);
|
||
62 | } |
||
63 | return this._containsValue(_f,_10,_11,_12); |
||
64 | },_containsValue:function(_13,_14,_15,_16){ |
||
65 | var _17=this.getValues(_13,_14); |
||
66 | for(var i=0;i<_17.length;++i){ |
||
67 | var _18=_17[i];
|
||
68 | if(typeof _18==="string"&&_16){ |
||
69 | return (_18.match(_16)!==null); |
||
70 | }else{
|
||
71 | if(_15===_18){
|
||
72 | return true; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | return false; |
||
77 | },isItem:function(_19){ |
||
78 | if(_19&&_19[this._storeProp]===this){ |
||
79 | return true; |
||
80 | } |
||
81 | return false; |
||
82 | },isItemLoaded:function(_1a){ |
||
83 | return this.isItem(_1a); |
||
84 | },loadItem:function(_1b){ |
||
85 | },getFeatures:function(){ |
||
86 | return this._features; |
||
87 | },close:function(_1c){ |
||
88 | },getLabel:function(_1d){ |
||
89 | return _1d[this._keyAttribute]; |
||
90 | },getLabelAttributes:function(_1e){ |
||
91 | return [this._keyAttribute]; |
||
92 | },_fetchItems:function(_1f,_20,_21){ |
||
93 | var _22=this; |
||
94 | var _23=function(_24,_25){ |
||
95 | var _26=null; |
||
96 | if(_24.query){
|
||
97 | _26=[]; |
||
98 | var _27=_24.queryOptions?_24.queryOptions.ignoreCase:false; |
||
99 | var _28={};
|
||
100 | for(var key in _24.query){ |
||
101 | var _29=_24.query[key];
|
||
102 | if(typeof _29==="string"){ |
||
103 | _28[key]=dojo.data.util.filter.patternToRegExp(_29,_27); |
||
104 | } |
||
105 | } |
||
106 | for(var i=0;i<_25.length;++i){ |
||
107 | var _2a=true; |
||
108 | var _2b=_25[i];
|
||
109 | for(var key in _24.query){ |
||
110 | var _29=_24.query[key];
|
||
111 | if(!_22._containsValue(_2b,key,_29,_28[key])){
|
||
112 | _2a=false;
|
||
113 | } |
||
114 | } |
||
115 | if(_2a){
|
||
116 | _26.push(_2b); |
||
117 | } |
||
118 | } |
||
119 | }else{
|
||
120 | if(_24.identity){
|
||
121 | _26=[]; |
||
122 | var _2c;
|
||
123 | for(var key in _25){ |
||
124 | _2c=_25[key]; |
||
125 | if(_2c[_22._keyAttribute]==_24.identity){
|
||
126 | _26.push(_2c); |
||
127 | break;
|
||
128 | } |
||
129 | } |
||
130 | }else{
|
||
131 | if(_25.length>0){ |
||
132 | _26=_25.slice(0,_25.length);
|
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | _20(_26,_24); |
||
137 | }; |
||
138 | if(this._loadFinished){ |
||
139 | _23(_1f,this._arrayOfAllItems);
|
||
140 | }else{
|
||
141 | if(this.url!==""){ |
||
142 | if(this._loadInProgress){ |
||
143 | this._queuedFetches.push({args:_1f,filter:_23}); |
||
144 | }else{
|
||
145 | this._loadInProgress=true; |
||
146 | var _2d={url:_22.url,handleAs:"json-comment-filtered",preventCache:this.urlPreventCache}; |
||
147 | var _2e=dojo.xhrGet(_2d);
|
||
148 | _2e.addCallback(function(_2f){
|
||
149 | _22._processData(_2f); |
||
150 | _23(_1f,_22._arrayOfAllItems); |
||
151 | _22._handleQueuedFetches(); |
||
152 | }); |
||
153 | _2e.addErrback(function(_30){
|
||
154 | _22._loadInProgress=false;
|
||
155 | throw _30;
|
||
156 | }); |
||
157 | } |
||
158 | }else{
|
||
159 | if(this._keyValueString){ |
||
160 | this._processData(eval(this._keyValueString)); |
||
161 | this._keyValueString=null; |
||
162 | _23(_1f,this._arrayOfAllItems);
|
||
163 | }else{
|
||
164 | if(this._keyValueVar){ |
||
165 | this._processData(this._keyValueVar); |
||
166 | this._keyValueVar=null; |
||
167 | _23(_1f,this._arrayOfAllItems);
|
||
168 | }else{
|
||
169 | throw new Error("dojox.data.KeyValueStore: No source data was provided as either URL, String, or Javascript variable data input."); |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | },_handleQueuedFetches:function(){ |
||
175 | if(this._queuedFetches.length>0){ |
||
176 | for(var i=0;i<this._queuedFetches.length;i++){ |
||
177 | var _31=this._queuedFetches[i]; |
||
178 | var _32=_31.filter;
|
||
179 | var _33=_31.args;
|
||
180 | if(_32){
|
||
181 | _32(_33,this._arrayOfAllItems);
|
||
182 | }else{
|
||
183 | this.fetchItemByIdentity(_31.args);
|
||
184 | } |
||
185 | } |
||
186 | this._queuedFetches=[];
|
||
187 | } |
||
188 | },_processData:function(_34){ |
||
189 | this._arrayOfAllItems=[];
|
||
190 | for(var i=0;i<_34.length;i++){ |
||
191 | this._arrayOfAllItems.push(this._createItem(_34[i])); |
||
192 | } |
||
193 | this._loadFinished=true; |
||
194 | this._loadInProgress=false; |
||
195 | },_createItem:function(_35){ |
||
196 | var _36={};
|
||
197 | _36[this._storeProp]=this; |
||
198 | for(var i in _35){ |
||
199 | _36[this._keyAttribute]=i;
|
||
200 | _36[this._valueAttribute]=_35[i];
|
||
201 | break;
|
||
202 | } |
||
203 | return _36;
|
||
204 | },getIdentity:function(_37){ |
||
205 | if(this.isItem(_37)){ |
||
206 | return _37[this._keyAttribute]; |
||
207 | } |
||
208 | return null; |
||
209 | },getIdentityAttributes:function(_38){ |
||
210 | return [this._keyAttribute]; |
||
211 | },fetchItemByIdentity:function(_39){ |
||
212 | _39.oldOnItem=_39.onItem; |
||
213 | _39.onItem=null;
|
||
214 | _39.onComplete=this._finishFetchItemByIdentity;
|
||
215 | this.fetch(_39);
|
||
216 | },_finishFetchItemByIdentity:function(_3a,_3b){ |
||
217 | var _3c=_3b.scope||dojo.global;
|
||
218 | if(_3a.length){
|
||
219 | _3b.oldOnItem.call(_3c,_3a[0]);
|
||
220 | }else{
|
||
221 | _3b.oldOnItem.call(_3c,null);
|
||
222 | } |
||
223 | }}); |
||
224 | dojo.extend(dojox.data.KeyValueStore,dojo.data.util.simpleFetch); |
||
225 | } |