root / trunk / web / dojo / dojox / data / ServiceStore.js
History | View | Annotate | Download (4.11 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.ServiceStore"]){ |
9 |
dojo._hasResource["dojox.data.ServiceStore"]=true; |
10 |
dojo.provide("dojox.data.ServiceStore");
|
11 |
dojo.declare("dojox.data.ServiceStore",dojox.data.ClientFilter||null,{service:null,constructor:function(_1){ |
12 |
this.byId=this.fetchItemByIdentity; |
13 |
this._index={};
|
14 |
if(_1){
|
15 |
dojo.mixin(this,_1);
|
16 |
} |
17 |
this.idAttribute=(_1&&_1.idAttribute)||(this.schema&&this.schema._idAttr); |
18 |
},schema:null,idAttribute:"id",labelAttribute:"label",syncMode:false,estimateCountFactor:1,getSchema:function(){ |
19 |
return this.schema; |
20 |
},loadLazyValues:true,getValue:function(_2,_3,_4){ |
21 |
var _5=_2[_3];
|
22 |
return _5||(_3 in _2?_5:_2._loadObject?(dojox.rpc._sync=true)&&arguments.callee.call(this,dojox.data.ServiceStore.prototype.loadItem({item:_2})||{},_3,_4):_4); |
23 |
},getValues:function(_6,_7){ |
24 |
var _8=this.getValue(_6,_7); |
25 |
if(_8 instanceof Array){ |
26 |
return _8;
|
27 |
} |
28 |
if(!this.isItemLoaded(_8)){ |
29 |
dojox.rpc._sync=true;
|
30 |
_8=this.loadItem({item:_8}); |
31 |
} |
32 |
return _8 instanceof Array?_8:_8===undefined?[]:[_8]; |
33 |
},getAttributes:function(_9){ |
34 |
var _a=[];
|
35 |
for(var i in _9){ |
36 |
if(_9.hasOwnProperty(i)&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")){ |
37 |
_a.push(i); |
38 |
} |
39 |
} |
40 |
return _a;
|
41 |
},hasAttribute:function(_b,_c){ |
42 |
return _c in _b; |
43 |
},containsValue:function(_d,_e,_f){ |
44 |
return dojo.indexOf(this.getValues(_d,_e),_f)>-1; |
45 |
},isItem:function(_10){ |
46 |
return (typeof _10=="object")&&_10&&!(_10 instanceof Date); |
47 |
},isItemLoaded:function(_11){ |
48 |
return _11&&!_11._loadObject;
|
49 |
},loadItem:function(_12){ |
50 |
var _13;
|
51 |
if(_12.item._loadObject){
|
52 |
_12.item._loadObject(function(_14){
|
53 |
_13=_14; |
54 |
delete _13._loadObject;
|
55 |
var _15=_14 instanceof Error?_12.onError:_12.onItem; |
56 |
if(_15){
|
57 |
_15.call(_12.scope,_14); |
58 |
} |
59 |
}); |
60 |
}else{
|
61 |
if(_12.onItem){
|
62 |
_12.onItem.call(_12.scope,_12.item); |
63 |
} |
64 |
} |
65 |
return _13;
|
66 |
},_currentId:0,_processResults:function(_16,_17){ |
67 |
if(_16&&typeof _16=="object"){ |
68 |
var id=_16.__id;
|
69 |
if(!id){
|
70 |
if(this.idAttribute){ |
71 |
id=_16[this.idAttribute];
|
72 |
}else{
|
73 |
id=this._currentId++;
|
74 |
} |
75 |
if(id!==undefined){ |
76 |
var _18=this._index[id]; |
77 |
if(_18){
|
78 |
for(var j in _18){ |
79 |
delete _18[j];
|
80 |
} |
81 |
_16=dojo.mixin(_18,_16); |
82 |
} |
83 |
_16.__id=id; |
84 |
this._index[id]=_16;
|
85 |
} |
86 |
} |
87 |
for(var i in _16){ |
88 |
_16[i]=this._processResults(_16[i],_17).items;
|
89 |
} |
90 |
var _19=_16.length;
|
91 |
} |
92 |
return {totalCount:_17.request.count==_19?(_17.request.start||0)+_19*this.estimateCountFactor:_19,items:_16}; |
93 |
},close:function(_1a){ |
94 |
return _1a&&_1a.abort&&_1a.abort();
|
95 |
},fetch:function(_1b){ |
96 |
_1b=_1b||{}; |
97 |
if("syncMode" in _1b?_1b.syncMode:this.syncMode){ |
98 |
dojox.rpc._sync=true;
|
99 |
} |
100 |
var _1c=this; |
101 |
var _1d=_1b.scope||_1c;
|
102 |
var _1e=this.cachingFetch?this.cachingFetch(_1b):this._doQuery(_1b); |
103 |
_1e.request=_1b; |
104 |
_1e.addCallback(function(_1f){
|
105 |
if(_1b.clientFetch){
|
106 |
_1f=_1c.clientSideFetch({query:_1b.clientFetch,sort:_1b.sort,start:_1b.start,count:_1b.count},_1f); |
107 |
} |
108 |
var _20=_1c._processResults(_1f,_1e);
|
109 |
_1f=_1b.results=_20.items; |
110 |
if(_1b.onBegin){
|
111 |
_1b.onBegin.call(_1d,_20.totalCount,_1b); |
112 |
} |
113 |
if(_1b.onItem){
|
114 |
for(var i=0;i<_1f.length;i++){ |
115 |
_1b.onItem.call(_1d,_1f[i],_1b); |
116 |
} |
117 |
} |
118 |
if(_1b.onComplete){
|
119 |
_1b.onComplete.call(_1d,_1b.onItem?null:_1f,_1b);
|
120 |
} |
121 |
return _1f;
|
122 |
}); |
123 |
_1e.addErrback(_1b.onError&&function(err){
|
124 |
return _1b.onError.call(_1d,err,_1b);
|
125 |
}); |
126 |
_1b.abort=function(){ |
127 |
_1e.cancel(); |
128 |
}; |
129 |
_1b.store=this;
|
130 |
return _1b;
|
131 |
},_doQuery:function(_21){ |
132 |
var _22=typeof _21.queryStr=="string"?_21.queryStr:_21.query; |
133 |
return this.service(_22); |
134 |
},getFeatures:function(){ |
135 |
return {"dojo.data.api.Read":true,"dojo.data.api.Identity":true,"dojo.data.api.Schema":this.schema}; |
136 |
},getLabel:function(_23){ |
137 |
return this.getValue(_23,this.labelAttribute); |
138 |
},getLabelAttributes:function(_24){ |
139 |
return [this.labelAttribute]; |
140 |
},getIdentity:function(_25){ |
141 |
return _25.__id;
|
142 |
},getIdentityAttributes:function(_26){ |
143 |
return [this.idAttribute]; |
144 |
},fetchItemByIdentity:function(_27){ |
145 |
var _28=this._index[(_27._prefix||"")+_27.identity]; |
146 |
if(_28){
|
147 |
if(_28._loadObject){
|
148 |
_27.item=_28; |
149 |
return this.loadItem(_27); |
150 |
}else{
|
151 |
if(_27.onItem){
|
152 |
_27.onItem.call(_27.scope,_28); |
153 |
} |
154 |
} |
155 |
}else{
|
156 |
return this.fetch({query:_27.identity,onComplete:_27.onItem,onError:_27.onError,scope:_27.scope}).results; |
157 |
} |
158 |
return _28;
|
159 |
}}); |
160 |
} |