root / trunk / web / dojo / dojox / rpc / JsonRest.js @ 9
History | View | Annotate | Download (7.44 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.rpc.JsonRest"]){ |
9 |
dojo._hasResource["dojox.rpc.JsonRest"]=true; |
10 |
dojo.provide("dojox.rpc.JsonRest");
|
11 |
dojo.require("dojox.json.ref");
|
12 |
dojo.require("dojox.rpc.Rest");
|
13 |
(function(){
|
14 |
var _1=[];
|
15 |
var _2=dojox.rpc.Rest;
|
16 |
var jr;
|
17 |
function _3(_4,_5,_6,_7){ |
18 |
var _8=_5.ioArgs&&_5.ioArgs.xhr&&_5.ioArgs.xhr.getResponseHeader("Last-Modified"); |
19 |
if(_8&&_2._timeStamps){
|
20 |
_2._timeStamps[_7]=_8; |
21 |
} |
22 |
var _9=_4._schema&&_4._schema.hrefProperty;
|
23 |
if(_9){
|
24 |
dojox.json.ref.refAttribute=_9; |
25 |
} |
26 |
_6=_6&&dojox.json.ref.resolveJson(_6,{defaultId:_7,index:_2._index,timeStamps:_8&&_2._timeStamps,time:_8,idPrefix:_4.servicePath.replace(/[^\/]*$/,""),idAttribute:jr.getIdAttribute(_4),schemas:jr.schemas,loader:jr._loader,idAsRef:_4.idAsRef,assignAbsoluteIds:true}); |
27 |
dojox.json.ref.refAttribute="$ref";
|
28 |
return _6;
|
29 |
}; |
30 |
jr=dojox.rpc.JsonRest={serviceClass:dojox.rpc.Rest,conflictDateHeader:"If-Unmodified-Since",commit:function(_a){ |
31 |
_a=_a||{}; |
32 |
var _b=[];
|
33 |
var _c={};
|
34 |
var _d=[];
|
35 |
for(var i=0;i<_1.length;i++){ |
36 |
var _e=_1[i];
|
37 |
var _f=_e.object;
|
38 |
var old=_e.old;
|
39 |
var _10=false; |
40 |
if(!(_a.service&&(_f||old)&&(_f||old).__id.indexOf(_a.service.servicePath))&&_e.save){
|
41 |
delete _f.__isDirty;
|
42 |
if(_f){
|
43 |
if(old){
|
44 |
var _11;
|
45 |
if((_11=_f.__id.match(/(.*)#.*/))){ |
46 |
_f=_2._index[_11[1]];
|
47 |
} |
48 |
if(!(_f.__id in _c)){ |
49 |
_c[_f.__id]=_f; |
50 |
if(_a.incrementalUpdates&&!_11){
|
51 |
var _12=(typeof _a.incrementalUpdates=="function"?_a.incrementalUpdates:function(){ |
52 |
_12={}; |
53 |
for(var j in _f){ |
54 |
if(_f.hasOwnProperty(j)){
|
55 |
if(_f[j]!==old[j]){
|
56 |
_12[j]=_f[j]; |
57 |
} |
58 |
}else{
|
59 |
if(old.hasOwnProperty(j)){
|
60 |
return null; |
61 |
} |
62 |
} |
63 |
} |
64 |
return _12;
|
65 |
})(_f,old); |
66 |
} |
67 |
if(_12){
|
68 |
_b.push({method:"post",target:_f,content:_12}); |
69 |
}else{
|
70 |
_b.push({method:"put",target:_f,content:_f}); |
71 |
} |
72 |
} |
73 |
}else{
|
74 |
var _13=jr.getServiceAndId(_f.__id).service;
|
75 |
var _14=jr.getIdAttribute(_13);
|
76 |
if((_14 in _f)&&!_a.alwaysPostNewItems){ |
77 |
_b.push({method:"put",target:_f,content:_f}); |
78 |
}else{
|
79 |
_b.push({method:"post",target:{__id:_13.servicePath},content:_f}); |
80 |
} |
81 |
} |
82 |
}else{
|
83 |
if(old){
|
84 |
_b.push({method:"delete",target:old}); |
85 |
} |
86 |
} |
87 |
_d.push(_e); |
88 |
_1.splice(i--,1);
|
89 |
} |
90 |
} |
91 |
dojo.connect(_a,"onError",function(){ |
92 |
if(_a.revertOnError!==false){ |
93 |
var _15=_1;
|
94 |
_1=_d; |
95 |
var _16=0; |
96 |
jr.revert(); |
97 |
_1=_15; |
98 |
}else{
|
99 |
_1=dirtyObject.concat(_d); |
100 |
} |
101 |
}); |
102 |
jr.sendToServer(_b,_a); |
103 |
return _b;
|
104 |
},sendToServer:function(_17,_18){ |
105 |
var _19;
|
106 |
var _1a=dojo.xhr;
|
107 |
var _1b=_17.length;
|
108 |
var i,_1c;
|
109 |
var _1d;
|
110 |
var _1e=this.conflictDateHeader; |
111 |
dojo.xhr=function(_1f,_20){ |
112 |
_20.headers=_20.headers||{}; |
113 |
_20.headers["Transaction"]=_17.length-1==i?"commit":"open"; |
114 |
if(_1e&&_1d){
|
115 |
_20.headers[_1e]=_1d; |
116 |
} |
117 |
if(_1c){
|
118 |
_20.headers["Content-ID"]="<"+_1c+">"; |
119 |
} |
120 |
return _1a.apply(dojo,arguments); |
121 |
}; |
122 |
for(i=0;i<_17.length;i++){ |
123 |
var _21=_17[i];
|
124 |
dojox.rpc.JsonRest._contentId=_21.content&&_21.content.__id; |
125 |
var _22=_21.method=="post"; |
126 |
_1d=_21.method=="put"&&_2._timeStamps[_21.content.__id];
|
127 |
if(_1d){
|
128 |
_2._timeStamps[_21.content.__id]=(new Date())+""; |
129 |
} |
130 |
_1c=_22&&dojox.rpc.JsonRest._contentId; |
131 |
var _23=jr.getServiceAndId(_21.target.__id);
|
132 |
var _24=_23.service;
|
133 |
var dfd=_21.deferred=_24[_21.method](_23.id.replace(/#/,""),dojox.json.ref.toJson(_21.content,false,_24.servicePath,true)); |
134 |
(function(_25,dfd,_26){
|
135 |
dfd.addCallback(function(_27){
|
136 |
try{
|
137 |
var _28=dfd.ioArgs.xhr&&dfd.ioArgs.xhr.getResponseHeader("Location"); |
138 |
if(_28){
|
139 |
var _29=_28.match(/(^\w+:\/\/)/)&&_28.indexOf(_26.servicePath); |
140 |
_28=_29>0?_28.substring(_29):(_26.servicePath+_28).replace(/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/,"$2$3"); |
141 |
_25.__id=_28; |
142 |
_2._index[_28]=_25; |
143 |
} |
144 |
_27=_3(_26,dfd,_27,_25&&_25.__id); |
145 |
} |
146 |
catch(e){
|
147 |
} |
148 |
if(!(--_1b)){
|
149 |
if(_18.onComplete){
|
150 |
_18.onComplete.call(_18.scope,_17); |
151 |
} |
152 |
} |
153 |
return _27;
|
154 |
}); |
155 |
})(_21.content,dfd,_24); |
156 |
dfd.addErrback(function(_2a){
|
157 |
_1b=-1;
|
158 |
_18.onError.call(_18.scope,_2a); |
159 |
}); |
160 |
} |
161 |
dojo.xhr=_1a; |
162 |
},getDirtyObjects:function(){ |
163 |
return _1;
|
164 |
},revert:function(_2b){ |
165 |
for(var i=_1.length;i>0;){ |
166 |
i--; |
167 |
var _2c=_1[i];
|
168 |
var _2d=_2c.object;
|
169 |
var old=_2c.old;
|
170 |
var _2e=dojox.data._getStoreForItem(_2d||old);
|
171 |
if(!(_2b&&(_2d||old)&&(_2d||old).__id.indexOf(_2b.servicePath))){
|
172 |
if(_2d&&old){
|
173 |
for(var j in old){ |
174 |
if(old.hasOwnProperty(j)&&_2d[j]!==old[j]){
|
175 |
if(_2e){
|
176 |
_2e.onSet(_2d,j,_2d[j],old[j]); |
177 |
} |
178 |
_2d[j]=old[j]; |
179 |
} |
180 |
} |
181 |
for(j in _2d){ |
182 |
if(!old.hasOwnProperty(j)){
|
183 |
if(_2e){
|
184 |
_2e.onSet(_2d,j,_2d[j]); |
185 |
} |
186 |
delete _2d[j];
|
187 |
} |
188 |
} |
189 |
}else{
|
190 |
if(!old){
|
191 |
if(_2e){
|
192 |
_2e.onDelete(_2d); |
193 |
} |
194 |
}else{
|
195 |
if(_2e){
|
196 |
_2e.onNew(old); |
197 |
} |
198 |
} |
199 |
} |
200 |
delete (_2d||old).__isDirty;
|
201 |
_1.splice(i,1);
|
202 |
} |
203 |
} |
204 |
},changing:function(_2f,_30){ |
205 |
if(!_2f.__id){
|
206 |
return;
|
207 |
} |
208 |
_2f.__isDirty=true;
|
209 |
for(var i=0;i<_1.length;i++){ |
210 |
var _31=_1[i];
|
211 |
if(_2f==_31.object){
|
212 |
if(_30){
|
213 |
_31.object=false;
|
214 |
if(!this._saveNotNeeded){ |
215 |
_31.save=true;
|
216 |
} |
217 |
} |
218 |
return;
|
219 |
} |
220 |
} |
221 |
var old=_2f instanceof Array?[]:{}; |
222 |
for(i in _2f){ |
223 |
if(_2f.hasOwnProperty(i)){
|
224 |
old[i]=_2f[i]; |
225 |
} |
226 |
} |
227 |
_1.push({object:!_30&&_2f,old:old,save:!this._saveNotNeeded}); |
228 |
},deleteObject:function(_32){ |
229 |
this.changing(_32,true); |
230 |
},getConstructor:function(_33,_34){ |
231 |
if(typeof _33=="string"){ |
232 |
var _35=_33;
|
233 |
_33=new dojox.rpc.Rest(_33,true); |
234 |
this.registerService(_33,_35,_34);
|
235 |
} |
236 |
if(_33._constructor){
|
237 |
return _33._constructor;
|
238 |
} |
239 |
_33._constructor=function(_36){ |
240 |
var _37=this; |
241 |
var _38=arguments; |
242 |
var _39;
|
243 |
var _3a;
|
244 |
function _3b(_3c){ |
245 |
if(_3c){
|
246 |
_3b(_3c["extends"]);
|
247 |
_39=_3c.properties; |
248 |
for(var i in _39){ |
249 |
var _3d=_39[i];
|
250 |
if(_3d&&(typeof _3d=="object")&&("default" in _3d)){ |
251 |
_37[i]=_3d["default"];
|
252 |
} |
253 |
} |
254 |
} |
255 |
if(_3c&&_3c.prototype&&_3c.prototype.initialize){
|
256 |
_3a=true;
|
257 |
_3c.prototype.initialize.apply(_37,_38); |
258 |
} |
259 |
}; |
260 |
_3b(_33._schema); |
261 |
if(!_3a&&_36&&typeof _36=="object"){ |
262 |
dojo.mixin(_37,_36); |
263 |
} |
264 |
var _3e=jr.getIdAttribute(_33);
|
265 |
_2._index[this.__id=this.__clientId=_33.servicePath+(this[_3e]||Math.random().toString(16).substring(2,14)+"@"+((dojox.rpc.Client&&dojox.rpc.Client.clientId)||"client"))]=this; |
266 |
if(dojox.json.schema&&_39){
|
267 |
dojox.json.schema.mustBeValid(dojox.json.schema.validate(this,_33._schema));
|
268 |
} |
269 |
_1.push({object:this,save:true}); |
270 |
}; |
271 |
return dojo.mixin(_33._constructor,_33._schema,{load:_33}); |
272 |
},fetch:function(_3f){ |
273 |
var _40=jr.getServiceAndId(_3f);
|
274 |
return this.byId(_40.service,_40.id); |
275 |
},getIdAttribute:function(_41){ |
276 |
var _42=_41._schema;
|
277 |
var _43;
|
278 |
if(_42){
|
279 |
if(!(_43=_42._idAttr)){
|
280 |
for(var i in _42.properties){ |
281 |
if(_42.properties[i].identity||(_42.properties[i].link=="self")){ |
282 |
_42._idAttr=_43=i; |
283 |
} |
284 |
} |
285 |
} |
286 |
} |
287 |
return _43||"id"; |
288 |
},getServiceAndId:function(_44){ |
289 |
var _45=""; |
290 |
for(var _46 in jr.services){ |
291 |
if((_44.substring(0,_46.length)==_46)&&(_46.length>=_45.length)){ |
292 |
_45=_46; |
293 |
} |
294 |
} |
295 |
if(_45){
|
296 |
return {service:jr.services[_45],id:_44.substring(_45.length)}; |
297 |
} |
298 |
var _47=_44.match(/^(.*\/)([^\/]*)$/); |
299 |
return {service:new jr.serviceClass(_47[1],true),id:_47[2]}; |
300 |
},services:{},schemas:{},registerService:function(_48,_49,_4a){ |
301 |
_49=_48.servicePath=_49||_48.servicePath; |
302 |
_48._schema=jr.schemas[_49]=_4a||_48._schema||{}; |
303 |
jr.services[_49]=_48; |
304 |
},byId:function(_4b,id){ |
305 |
var _4c,_4d=_2._index[(_4b.servicePath||"")+id]; |
306 |
if(_4d&&!_4d._loadObject){
|
307 |
_4c=new dojo.Deferred();
|
308 |
_4c.callback(_4d); |
309 |
return _4c;
|
310 |
} |
311 |
return this.query(_4b,id); |
312 |
},query:function(_4e,id,_4f){ |
313 |
var _50=_4e(id,_4f);
|
314 |
_50.addCallback(function(_51){
|
315 |
if(_51.nodeType&&_51.cloneNode){
|
316 |
return _51;
|
317 |
} |
318 |
return _3(_4e,_50,_51,typeof id!="string"||(_4f&&(_4f.start||_4f.count))?undefined:id); |
319 |
}); |
320 |
return _50;
|
321 |
},_loader:function(_52){ |
322 |
var _53=jr.getServiceAndId(this.__id); |
323 |
var _54=this; |
324 |
jr.query(_53.service,_53.id).addBoth(function(_55){
|
325 |
if(_55==_54){
|
326 |
delete _55.$ref; |
327 |
delete _55._loadObject;
|
328 |
}else{
|
329 |
_54._loadObject=function(_56){ |
330 |
_56(_55); |
331 |
}; |
332 |
} |
333 |
_52(_55); |
334 |
}); |
335 |
},isDirty:function(_57){ |
336 |
if(!_57){
|
337 |
return !!_1.length;
|
338 |
} |
339 |
return _57.__isDirty;
|
340 |
}}; |
341 |
})(); |
342 |
} |