root / trunk / web / dojo / dojox / data / CdfStore.js @ 10
History | View | Annotate | Download (6.6 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.CdfStore"]){ |
9 |
dojo._hasResource["dojox.data.CdfStore"]=true; |
10 |
dojo.provide("dojox.data.CdfStore");
|
11 |
dojo.require("dojo.data.util.sorter");
|
12 |
dojox.data.ASYNC_MODE=0;
|
13 |
dojox.data.SYNC_MODE=1;
|
14 |
dojo.declare("dojox.data.CdfStore",null,{identity:"jsxid",url:"",xmlStr:"",data:null,label:"",mode:dojox.data.ASYNC_MODE,constructor:function(_1){ |
15 |
if(_1){
|
16 |
this.url=_1.url;
|
17 |
this.xmlStr=_1.xmlStr||_1.str;
|
18 |
if(_1.data){
|
19 |
this.xmlStr=this._makeXmlString(_1.data); |
20 |
} |
21 |
this.identity=_1.identity||this.identity; |
22 |
this.label=_1.label||this.label; |
23 |
this.mode=_1.mode!==undefined?_1.mode:this.mode; |
24 |
} |
25 |
this._modifiedItems={};
|
26 |
this.byId=this.fetchItemByIdentity; |
27 |
},getValue:function(_2,_3,_4){ |
28 |
return _2.getAttribute(_3)||_4;
|
29 |
},getValues:function(_5,_6){ |
30 |
var v=this.getValue(_5,_6,[]); |
31 |
return dojo.isArray(v)?v:[v];
|
32 |
},getAttributes:function(_7){ |
33 |
return _7.getAttributeNames();
|
34 |
},hasAttribute:function(_8,_9){ |
35 |
return (this.getValue(_8,_9)!==undefined); |
36 |
},hasProperty:function(_a,_b){ |
37 |
return this.hasAttribute(_a,_b); |
38 |
},containsValue:function(_c,_d,_e){ |
39 |
var _f=this.getValues(_c,_d); |
40 |
for(var i=0;i<_f.length;i++){ |
41 |
if(_f[i]===null){ |
42 |
continue;
|
43 |
} |
44 |
if((typeof _e==="string")){ |
45 |
if(_f[i].toString&&_f[i].toString()===_e){
|
46 |
return true; |
47 |
} |
48 |
}else{
|
49 |
if(_f[i]===_e){
|
50 |
return true; |
51 |
} |
52 |
} |
53 |
} |
54 |
return false; |
55 |
},isItem:function(_10){ |
56 |
if(_10.getClass&&_10.getClass().equals(jsx3.xml.Entity.jsxclass)){
|
57 |
return true; |
58 |
} |
59 |
return false; |
60 |
},isItemLoaded:function(_11){ |
61 |
return this.isItem(_11); |
62 |
},loadItem:function(_12){ |
63 |
},getFeatures:function(){ |
64 |
return {"dojo.data.api.Read":true,"dojo.data.api.Write":true,"dojo.data.api.Identity":true}; |
65 |
},getLabel:function(_13){ |
66 |
if((this.label!=="")&&this.isItem(_13)){ |
67 |
var _14=this.getValue(_13,this.label); |
68 |
if(_14){
|
69 |
return _14.toString();
|
70 |
} |
71 |
} |
72 |
return undefined; |
73 |
},getLabelAttributes:function(_15){ |
74 |
if(this.label!==""){ |
75 |
return [this.label]; |
76 |
} |
77 |
return null; |
78 |
},fetch:function(_16){ |
79 |
_16=_16||{}; |
80 |
if(!_16.store){
|
81 |
_16.store=this;
|
82 |
} |
83 |
if(_16.mode!==undefined){ |
84 |
this.mode=_16.mode;
|
85 |
} |
86 |
var _17=this; |
87 |
var _18=function(_19){ |
88 |
if(_16.onError){
|
89 |
var _1a=_16.scope||dojo.global;
|
90 |
_16.onError.call(_1a,_19,_16); |
91 |
}else{
|
92 |
console.error("cdfStore Error:",_19);
|
93 |
} |
94 |
}; |
95 |
var _1b=function(_1c,_1d){ |
96 |
_1d=_1d||_16; |
97 |
var _1e=_1d.abort||null; |
98 |
var _1f=false; |
99 |
var _20=_1d.start?_1d.start:0; |
100 |
var _21=(_1d.count&&(_1d.count!==Infinity))?(_20+_1d.count):_1c.length; |
101 |
_1d.abort=function(){ |
102 |
_1f=true;
|
103 |
if(_1e){
|
104 |
_1e.call(_1d); |
105 |
} |
106 |
}; |
107 |
var _22=_1d.scope||dojo.global;
|
108 |
if(!_1d.store){
|
109 |
_1d.store=_17; |
110 |
} |
111 |
if(_1d.onBegin){
|
112 |
_1d.onBegin.call(_22,_1c.length,_1d); |
113 |
} |
114 |
if(_1d.sort){
|
115 |
_1c.sort(dojo.data.util.sorter.createSortFunction(_1d.sort,_17)); |
116 |
} |
117 |
if(_1d.onItem){
|
118 |
for(var i=_20;(i<_1c.length)&&(i<_21);++i){ |
119 |
var _23=_1c[i];
|
120 |
if(!_1f){
|
121 |
_1d.onItem.call(_22,_23,_1d); |
122 |
} |
123 |
} |
124 |
} |
125 |
if(_1d.onComplete&&!_1f){
|
126 |
if(!_1d.onItem){
|
127 |
_1c=_1c.slice(_20,_21); |
128 |
if(_1d.byId){
|
129 |
_1c=_1c[0];
|
130 |
} |
131 |
} |
132 |
_1d.onComplete.call(_22,_1c,_1d); |
133 |
}else{
|
134 |
_1c=_1c.slice(_20,_21); |
135 |
if(_1d.byId){
|
136 |
_1c=_1c[0];
|
137 |
} |
138 |
} |
139 |
return _1c;
|
140 |
}; |
141 |
if(!this.url&&!this.data&&!this.xmlStr){ |
142 |
_18(new Error("No URL or data specified.")); |
143 |
return false; |
144 |
} |
145 |
var _24=_16||"*"; |
146 |
if(this.mode==dojox.data.SYNC_MODE){ |
147 |
var res=this._loadCDF(); |
148 |
if(res instanceof Error){ |
149 |
if(_16.onError){
|
150 |
_16.onError.call(_16.scope||dojo.global,res,_16); |
151 |
}else{
|
152 |
console.error("CdfStore Error:",res);
|
153 |
} |
154 |
return res;
|
155 |
} |
156 |
this.cdfDoc=res;
|
157 |
var _25=this._getItems(this.cdfDoc,_24); |
158 |
if(_25&&_25.length>0){ |
159 |
_25=_1b(_25,_16); |
160 |
}else{
|
161 |
_25=_1b([],_16); |
162 |
} |
163 |
return _25;
|
164 |
}else{
|
165 |
var dfd=this._loadCDF(); |
166 |
dfd.addCallbacks(dojo.hitch(this,function(_26){ |
167 |
var _27=this._getItems(this.cdfDoc,_24); |
168 |
if(_27&&_27.length>0){ |
169 |
_1b(_27,_16); |
170 |
}else{
|
171 |
_1b([],_16); |
172 |
} |
173 |
}),dojo.hitch(this,function(err){ |
174 |
_18(err,_16); |
175 |
})); |
176 |
return dfd;
|
177 |
} |
178 |
},_loadCDF:function(){ |
179 |
var dfd=new dojo.Deferred(); |
180 |
if(this.cdfDoc){ |
181 |
if(this.mode==dojox.data.SYNC_MODE){ |
182 |
return this.cdfDoc; |
183 |
}else{
|
184 |
setTimeout(dojo.hitch(this,function(){ |
185 |
dfd.callback(this.cdfDoc);
|
186 |
}),0);
|
187 |
return dfd;
|
188 |
} |
189 |
} |
190 |
this.cdfDoc=jsx3.xml.CDF.Document.newDocument();
|
191 |
this.cdfDoc.subscribe("response",this,function(evt){ |
192 |
dfd.callback(this.cdfDoc);
|
193 |
}); |
194 |
this.cdfDoc.subscribe("error",this,function(err){ |
195 |
dfd.errback(err); |
196 |
}); |
197 |
this.cdfDoc.setAsync(!this.mode); |
198 |
if(this.url){ |
199 |
this.cdfDoc.load(this.url); |
200 |
}else{
|
201 |
if(this.xmlStr){ |
202 |
this.cdfDoc.loadXML(this.xmlStr); |
203 |
if(this.cdfDoc.getError().code){ |
204 |
return new Error(this.cdfDoc.getError().description); |
205 |
} |
206 |
} |
207 |
} |
208 |
if(this.mode==dojox.data.SYNC_MODE){ |
209 |
return this.cdfDoc; |
210 |
}else{
|
211 |
return dfd;
|
212 |
} |
213 |
},_getItems:function(_28,_29){ |
214 |
var itr=_28.selectNodes(_29.query,false,1); |
215 |
var _2a=[];
|
216 |
while(itr.hasNext()){
|
217 |
_2a.push(itr.next()); |
218 |
} |
219 |
return _2a;
|
220 |
},close:function(_2b){ |
221 |
},newItem:function(_2c,_2d){ |
222 |
_2c=(_2c||{}); |
223 |
if(_2c.tagName){
|
224 |
if(_2c.tagName!="record"){ |
225 |
console.warn("Only record inserts are supported at this time");
|
226 |
} |
227 |
delete _2c.tagName;
|
228 |
} |
229 |
_2c.jsxid=_2c.jsxid||this.cdfDoc.getKey();
|
230 |
if(this.isItem(_2d)){ |
231 |
_2d=this.getIdentity(_2d);
|
232 |
} |
233 |
var _2e=this.cdfDoc.insertRecord(_2c,_2d); |
234 |
this._makeDirty(_2e);
|
235 |
return _2e;
|
236 |
},deleteItem:function(_2f){ |
237 |
this.cdfDoc.deleteRecord(this.getIdentity(_2f)); |
238 |
this._makeDirty(_2f);
|
239 |
return true; |
240 |
},setValue:function(_30,_31,_32){ |
241 |
this._makeDirty(_30);
|
242 |
_30.setAttribute(_31,_32); |
243 |
return true; |
244 |
},setValues:function(_33,_34,_35){ |
245 |
this._makeDirty(_33);
|
246 |
console.warn("cdfStore.setValues only partially implemented.");
|
247 |
return _33.setAttribute(_34,_35);
|
248 |
},unsetAttribute:function(_36,_37){ |
249 |
this._makeDirty(_36);
|
250 |
_36.removeAttribute(_37); |
251 |
return true; |
252 |
},revert:function(){ |
253 |
delete this.cdfDoc; |
254 |
this._modifiedItems={};
|
255 |
return true; |
256 |
},isDirty:function(_38){ |
257 |
if(_38){
|
258 |
return !!this._modifiedItems[this.getIdentity(_38)]; |
259 |
}else{
|
260 |
var _39=false; |
261 |
for(var nm in this._modifiedItems){ |
262 |
_39=true;
|
263 |
break;
|
264 |
} |
265 |
return _39;
|
266 |
} |
267 |
},_makeDirty:function(_3a){ |
268 |
var id=this.getIdentity(_3a); |
269 |
this._modifiedItems[id]=_3a;
|
270 |
},_makeXmlString:function(obj){ |
271 |
var _3b=function(obj,_3c){ |
272 |
var _3d=""; |
273 |
var nm;
|
274 |
if(dojo.isArray(obj)){
|
275 |
for(var i=0;i<obj.length;i++){ |
276 |
_3d+=_3b(obj[i],_3c); |
277 |
} |
278 |
}else{
|
279 |
if(dojo.isObject(obj)){
|
280 |
_3d+="<"+_3c+" "; |
281 |
for(nm in obj){ |
282 |
if(!dojo.isObject(obj[nm])){
|
283 |
_3d+=nm+"=\""+obj[nm]+"\" "; |
284 |
} |
285 |
} |
286 |
_3d+=">";
|
287 |
for(nm in obj){ |
288 |
if(dojo.isObject(obj[nm])){
|
289 |
_3d+=_3b(obj[nm],nm); |
290 |
} |
291 |
} |
292 |
_3d+="</"+_3c+">"; |
293 |
} |
294 |
} |
295 |
return _3d;
|
296 |
}; |
297 |
return _3b(obj,"data"); |
298 |
},getIdentity:function(_3e){ |
299 |
return this.getValue(_3e,this.identity); |
300 |
},getIdentityAttributes:function(_3f){ |
301 |
return [this.identity]; |
302 |
},fetchItemByIdentity:function(_40){ |
303 |
if(dojo.isString(_40)){
|
304 |
var id=_40;
|
305 |
_40={query:"//record[@jsxid='"+id+"']",mode:dojox.data.SYNC_MODE}; |
306 |
}else{
|
307 |
if(_40){
|
308 |
_40.query="//record[@jsxid='"+_40.identity+"']"; |
309 |
} |
310 |
if(!_40.mode){
|
311 |
_40.mode=this.mode;
|
312 |
} |
313 |
} |
314 |
_40.byId=true;
|
315 |
return this.fetch(_40); |
316 |
},byId:function(_41){ |
317 |
}}); |
318 |
} |