root / trunk / web / dojo / dojox / data / JsonRestStore.js
History | View | Annotate | Download (6.38 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.JsonRestStore"]){ |
||
9 | dojo._hasResource["dojox.data.JsonRestStore"]=true; |
||
10 | dojo.provide("dojox.data.JsonRestStore");
|
||
11 | dojo.require("dojox.data.ServiceStore");
|
||
12 | dojo.require("dojox.rpc.JsonRest");
|
||
13 | dojo.declare("dojox.data.JsonRestStore",dojox.data.ServiceStore,{constructor:function(_1){ |
||
14 | dojo.connect(dojox.rpc.Rest._index,"onUpdate",this,function(_2,_3,_4,_5){ |
||
15 | var _6=this.service.servicePath; |
||
16 | if(!_2.__id){
|
||
17 | }else{
|
||
18 | if(_2.__id.substring(0,_6.length)==_6){ |
||
19 | this.onSet(_2,_3,_4,_5);
|
||
20 | } |
||
21 | } |
||
22 | }); |
||
23 | this.idAttribute=this.idAttribute||"id"; |
||
24 | if(typeof _1.target=="string"){ |
||
25 | _1.target=_1.target.match(/\/$/)||this.allowNoTrailingSlash?_1.target:(_1.target+"/"); |
||
26 | if(!this.service){ |
||
27 | this.service=dojox.rpc.JsonRest.services[_1.target]||dojox.rpc.Rest(_1.target,true); |
||
28 | } |
||
29 | } |
||
30 | dojox.rpc.JsonRest.registerService(this.service,_1.target,this.schema); |
||
31 | this.schema=this.service._schema=this.schema||this.service._schema||{}; |
||
32 | this.service._store=this; |
||
33 | this.service.idAsRef=this.idAsRef; |
||
34 | this.schema._idAttr=this.idAttribute; |
||
35 | var _7=dojox.rpc.JsonRest.getConstructor(this.service); |
||
36 | var _8=this; |
||
37 | this._constructor=function(_9){ |
||
38 | _7.call(this,_9);
|
||
39 | _8.onNew(this);
|
||
40 | }; |
||
41 | this._constructor.prototype=_7.prototype;
|
||
42 | this._index=dojox.rpc.Rest._index;
|
||
43 | },loadReferencedSchema:true,idAsRef:false,referenceIntegrity:true,target:"",allowNoTrailingSlash:false,newItem:function(_a,_b){ |
||
44 | _a=new this._constructor(_a); |
||
45 | if(_b){
|
||
46 | var _c=this.getValue(_b.parent,_b.attribute,[]); |
||
47 | _c=_c.concat([_a]); |
||
48 | _a.__parent=_c; |
||
49 | this.setValue(_b.parent,_b.attribute,_c);
|
||
50 | } |
||
51 | return _a;
|
||
52 | },deleteItem:function(_d){ |
||
53 | var _e=[];
|
||
54 | var _f=dojox.data._getStoreForItem(_d)||this; |
||
55 | if(this.referenceIntegrity){ |
||
56 | dojox.rpc.JsonRest._saveNotNeeded=true;
|
||
57 | var _10=dojox.rpc.Rest._index;
|
||
58 | var _11=function(_12){ |
||
59 | var _13;
|
||
60 | _e.push(_12); |
||
61 | _12.__checked=1;
|
||
62 | for(var i in _12){ |
||
63 | if(i.substring(0,2)!="__"){ |
||
64 | var _14=_12[i];
|
||
65 | if(_14==_d){
|
||
66 | if(_12!=_10){
|
||
67 | if(_12 instanceof Array){ |
||
68 | (_13=_13||[]).push(i); |
||
69 | }else{
|
||
70 | (dojox.data._getStoreForItem(_12)||_f).unsetAttribute(_12,i); |
||
71 | } |
||
72 | } |
||
73 | }else{
|
||
74 | if((typeof _14=="object")&&_14){ |
||
75 | if(!_14.__checked){
|
||
76 | _11(_14); |
||
77 | } |
||
78 | if(typeof _14.__checked=="object"&&_12!=_10){ |
||
79 | (dojox.data._getStoreForItem(_12)||_f).setValue(_12,i,_14.__checked); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | if(_13){
|
||
86 | i=_13.length; |
||
87 | _12=_12.__checked=_12.concat(); |
||
88 | while(i--){
|
||
89 | _12.splice(_13[i],1);
|
||
90 | } |
||
91 | return _12;
|
||
92 | } |
||
93 | return null; |
||
94 | }; |
||
95 | _11(_10); |
||
96 | dojox.rpc.JsonRest._saveNotNeeded=false;
|
||
97 | var i=0; |
||
98 | while(_e[i]){
|
||
99 | delete _e[i++].__checked;
|
||
100 | } |
||
101 | } |
||
102 | dojox.rpc.JsonRest.deleteObject(_d); |
||
103 | _f.onDelete(_d); |
||
104 | },changing:function(_15,_16){ |
||
105 | dojox.rpc.JsonRest.changing(_15,_16); |
||
106 | },setValue:function(_17,_18,_19){ |
||
107 | var old=_17[_18];
|
||
108 | var _1a=_17.__id?dojox.data._getStoreForItem(_17):this; |
||
109 | if(dojox.json.schema&&_1a.schema&&_1a.schema.properties){
|
||
110 | dojox.json.schema.mustBeValid(dojox.json.schema.checkPropertyChange(_19,_1a.schema.properties[_18])); |
||
111 | } |
||
112 | if(_18==_1a.idAttribute){
|
||
113 | throw new Error("Can not change the identity attribute for an item"); |
||
114 | } |
||
115 | _1a.changing(_17); |
||
116 | _17[_18]=_19; |
||
117 | if(_19&&!_19.__parent){
|
||
118 | _19.__parent=_17; |
||
119 | } |
||
120 | _1a.onSet(_17,_18,old,_19); |
||
121 | },setValues:function(_1b,_1c,_1d){ |
||
122 | if(!dojo.isArray(_1d)){
|
||
123 | throw new Error("setValues expects to be passed an Array object as its value"); |
||
124 | } |
||
125 | this.setValue(_1b,_1c,_1d);
|
||
126 | },unsetAttribute:function(_1e,_1f){ |
||
127 | this.changing(_1e);
|
||
128 | var old=_1e[_1f];
|
||
129 | delete _1e[_1f];
|
||
130 | this.onSet(_1e,_1f,old,undefined); |
||
131 | },save:function(_20){ |
||
132 | if(!(_20&&_20.global)){
|
||
133 | (_20=_20||{}).service=this.service;
|
||
134 | } |
||
135 | if("syncMode" in _20?_20.syncMode:this.syncMode){ |
||
136 | dojox.rpc._sync=true;
|
||
137 | } |
||
138 | var _21=dojox.rpc.JsonRest.commit(_20);
|
||
139 | this.serverVersion=this._updates&&this._updates.length; |
||
140 | return _21;
|
||
141 | },revert:function(_22){ |
||
142 | dojox.rpc.JsonRest.revert(_22&&_22.global&&this.service);
|
||
143 | },isDirty:function(_23){ |
||
144 | return dojox.rpc.JsonRest.isDirty(_23);
|
||
145 | },isItem:function(_24,_25){ |
||
146 | return _24&&_24.__id&&(_25||this.service==dojox.rpc.JsonRest.getServiceAndId(_24.__id).service); |
||
147 | },_doQuery:function(_26){ |
||
148 | var _27=typeof _26.queryStr=="string"?_26.queryStr:_26.query; |
||
149 | var _28=dojox.rpc.JsonRest.query(this.service,_27,_26); |
||
150 | var _29=this; |
||
151 | if(this.loadReferencedSchema){ |
||
152 | _28.addCallback(function(_2a){
|
||
153 | var _2b=_28.ioArgs&&_28.ioArgs.xhr&&_28.ioArgs.xhr.getResponseHeader("Content-Type"); |
||
154 | var _2c=_2b&&_2b.match(/definedby\s*=\s*([^;]*)/); |
||
155 | if(_2b&&!_2c){
|
||
156 | _2c=_28.ioArgs.xhr.getResponseHeader("Link");
|
||
157 | _2c=_2c&&_2c.match(/<([^>]*)>;\s*rel="?definedby"?/);
|
||
158 | } |
||
159 | _2c=_2c&&_2c[1];
|
||
160 | if(_2c){
|
||
161 | var _2d=dojox.rpc.JsonRest.getServiceAndId((_29.target+_2c).replace(/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/,"$2$3")); |
||
162 | var _2e=dojox.rpc.JsonRest.byId(_2d.service,_2d.id);
|
||
163 | _2e.addCallbacks(function(_2f){
|
||
164 | dojo.mixin(_29.schema,_2f); |
||
165 | return _2a;
|
||
166 | },function(_30){
|
||
167 | console.error(_30); |
||
168 | return _2a;
|
||
169 | }); |
||
170 | return _2e;
|
||
171 | } |
||
172 | return undefined; |
||
173 | }); |
||
174 | } |
||
175 | return _28;
|
||
176 | },_processResults:function(_31,_32){ |
||
177 | var _33=_31.length;
|
||
178 | return {totalCount:_32.fullLength||(_32.request.count==_33?(_32.request.start||0)+_33*2:_33),items:_31}; |
||
179 | },getConstructor:function(){ |
||
180 | return this._constructor; |
||
181 | },getIdentity:function(_34){ |
||
182 | var id=_34.__clientId||_34.__id;
|
||
183 | if(!id){
|
||
184 | return id;
|
||
185 | } |
||
186 | var _35=this.service.servicePath.replace(/[^\/]*$/,""); |
||
187 | return id.substring(0,_35.length)!=_35?id:id.substring(_35.length); |
||
188 | },fetchItemByIdentity:function(_36){ |
||
189 | var id=_36.identity;
|
||
190 | var _37=this; |
||
191 | if(id.toString().match(/^(\w*:)?\//)){ |
||
192 | var _38=dojox.rpc.JsonRest.getServiceAndId(id);
|
||
193 | _37=_38.service._store; |
||
194 | _36.identity=_38.id; |
||
195 | } |
||
196 | _36._prefix=_37.service.servicePath.replace(/[^\/]*$/,""); |
||
197 | return _37.inherited(arguments); |
||
198 | },onSet:function(){ |
||
199 | },onNew:function(){ |
||
200 | },onDelete:function(){ |
||
201 | },getFeatures:function(){ |
||
202 | var _39=this.inherited(arguments); |
||
203 | _39["dojo.data.api.Write"]=true; |
||
204 | _39["dojo.data.api.Notification"]=true; |
||
205 | return _39;
|
||
206 | },getParent:function(_3a){ |
||
207 | return _3a&&_3a.__parent;
|
||
208 | }}); |
||
209 | dojox.data.JsonRestStore.getStore=function(_3b,_3c){ |
||
210 | if(typeof _3b.target=="string"){ |
||
211 | _3b.target=_3b.target.match(/\/$/)||_3b.allowNoTrailingSlash?_3b.target:(_3b.target+"/"); |
||
212 | var _3d=(dojox.rpc.JsonRest.services[_3b.target]||{})._store;
|
||
213 | if(_3d){
|
||
214 | return _3d;
|
||
215 | } |
||
216 | } |
||
217 | return new (_3c||dojox.data.JsonRestStore)(_3b); |
||
218 | }; |
||
219 | dojox.data._getStoreForItem=function(_3e){ |
||
220 | if(_3e.__id){
|
||
221 | var _3f=dojox.rpc.JsonRest.getServiceAndId(_3e.__id);
|
||
222 | if(_3f&&_3f.service._store){
|
||
223 | return _3f.service._store;
|
||
224 | }else{
|
||
225 | var _40=_3e.__id.toString().match(/.*\//)[0]; |
||
226 | return new dojox.data.JsonRestStore({target:_40}); |
||
227 | } |
||
228 | } |
||
229 | return null; |
||
230 | }; |
||
231 | dojox.json.ref._useRefs=true;
|
||
232 | } |