root / trunk / web / dojo / dojox / rpc / OfflineRest.js @ 10
History | View | Annotate | Download (4.14 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.OfflineRest"]){ |
| 9 |
dojo._hasResource["dojox.rpc.OfflineRest"]=true; |
| 10 |
dojo.provide("dojox.rpc.OfflineRest");
|
| 11 |
dojo.require("dojox.data.ClientFilter");
|
| 12 |
dojo.require("dojox.rpc.Rest");
|
| 13 |
dojo.require("dojox.storage");
|
| 14 |
(function(){
|
| 15 |
var _1=dojox.rpc.Rest;
|
| 16 |
var _2="dojox_rpc_OfflineRest"; |
| 17 |
var _3;
|
| 18 |
var _4=_1._index;
|
| 19 |
dojox.storage.manager.addOnLoad(function(){
|
| 20 |
_3=dojox.storage.manager.available; |
| 21 |
for(var i in _4){ |
| 22 |
_5(_4[i],i); |
| 23 |
} |
| 24 |
}); |
| 25 |
var _6;
|
| 26 |
function _7(_8){ |
| 27 |
return _8.replace(/[^0-9A-Za-z_]/g,"_"); |
| 28 |
}; |
| 29 |
function _5(_9,id){ |
| 30 |
if(_3&&!_6&&(id||(_9&&_9.__id))){
|
| 31 |
dojox.storage.put(_7(id||_9.__id),typeof _9=="object"?dojox.json.ref.toJson(_9):_9,function(){ |
| 32 |
},_2); |
| 33 |
} |
| 34 |
}; |
| 35 |
function _a(_b){ |
| 36 |
return _b instanceof Error&&(_b.status==503||_b.status>12000||!_b.status); |
| 37 |
}; |
| 38 |
function _c(){ |
| 39 |
if(_3){
|
| 40 |
var _d=dojox.storage.get("dirty",_2); |
| 41 |
if(_d){
|
| 42 |
for(var _e in _d){ |
| 43 |
_f(_e,_d); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
}; |
| 48 |
var _10;
|
| 49 |
function _11(){ |
| 50 |
_10.sendChanges(); |
| 51 |
_10.downloadChanges(); |
| 52 |
}; |
| 53 |
var _12=setInterval(_11,15000); |
| 54 |
dojo.connect(document,"ononline",_11);
|
| 55 |
_10=dojox.rpc.OfflineRest={turnOffAutoSync:function(){
|
| 56 |
clearInterval(_12); |
| 57 |
},sync:_11,sendChanges:_c,downloadChanges:function(){ |
| 58 |
},addStore:function(_13,_14){ |
| 59 |
_10.stores.push(_13); |
| 60 |
_13.fetch({queryOptions:{cache:true},query:_14,onComplete:function(_15,_16){
|
| 61 |
_13._localBaseResults=_15; |
| 62 |
_13._localBaseFetch=_16; |
| 63 |
}}); |
| 64 |
}}; |
| 65 |
_10.stores=[]; |
| 66 |
var _17=_1._get;
|
| 67 |
_1._get=function(_18,id){ |
| 68 |
try{
|
| 69 |
_c(); |
| 70 |
if(window.navigator&&navigator.onLine===false){ |
| 71 |
throw new Error(); |
| 72 |
} |
| 73 |
var dfd=_17(_18,id);
|
| 74 |
} |
| 75 |
catch(e){
|
| 76 |
dfd=new dojo.Deferred();
|
| 77 |
dfd.errback(e); |
| 78 |
} |
| 79 |
var _19=dojox.rpc._sync;
|
| 80 |
dfd.addCallback(function(_1a){
|
| 81 |
_5(_1a,_18._getRequest(id).url); |
| 82 |
return _1a;
|
| 83 |
}); |
| 84 |
dfd.addErrback(function(_1b){
|
| 85 |
if(_3){
|
| 86 |
if(_a(_1b)){
|
| 87 |
var _1c={};
|
| 88 |
var _1d=function(id,_1e){ |
| 89 |
if(_1c[id]){
|
| 90 |
return _1e;
|
| 91 |
} |
| 92 |
var _1f=dojo.fromJson(dojox.storage.get(_7(id),_2))||_1e;
|
| 93 |
_1c[id]=_1f; |
| 94 |
for(var i in _1f){ |
| 95 |
var val=_1f[i];
|
| 96 |
id=val&&val.$ref;
|
| 97 |
if(id){
|
| 98 |
if(id.substring&&id.substring(0,4)=="cid:"){ |
| 99 |
id=id.substring(4);
|
| 100 |
} |
| 101 |
_1f[i]=_1d(id,val); |
| 102 |
} |
| 103 |
} |
| 104 |
if(_1f instanceof Array){ |
| 105 |
for(i=0;i<_1f.length;i++){ |
| 106 |
if(_1f[i]===undefined){ |
| 107 |
_1f.splice(i--,1);
|
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
return _1f;
|
| 112 |
}; |
| 113 |
_6=true;
|
| 114 |
var _20=_1d(_18._getRequest(id).url);
|
| 115 |
if(!_20){
|
| 116 |
return _1b;
|
| 117 |
} |
| 118 |
_6=false;
|
| 119 |
return _20;
|
| 120 |
}else{
|
| 121 |
return _1b;
|
| 122 |
} |
| 123 |
}else{
|
| 124 |
if(_19){
|
| 125 |
return new Error("Storage manager not loaded, can not continue"); |
| 126 |
} |
| 127 |
dfd=new dojo.Deferred();
|
| 128 |
dfd.addCallback(arguments.callee);
|
| 129 |
dojox.storage.manager.addOnLoad(function(){
|
| 130 |
dfd.callback(); |
| 131 |
}); |
| 132 |
return dfd;
|
| 133 |
} |
| 134 |
}); |
| 135 |
return dfd;
|
| 136 |
}; |
| 137 |
function _21(_22,_23,_24,_25,_26){ |
| 138 |
if(_22=="delete"){ |
| 139 |
dojox.storage.remove(_7(_23),_2); |
| 140 |
}else{
|
| 141 |
dojox.storage.put(_7(_24),_25,function(){
|
| 142 |
},_2); |
| 143 |
} |
| 144 |
var _27=_26&&_26._store;
|
| 145 |
if(_27){
|
| 146 |
_27.updateResultSet(_27._localBaseResults,_27._localBaseFetch); |
| 147 |
dojox.storage.put(_7(_26._getRequest(_27._localBaseFetch.query).url),dojox.json.ref.toJson(_27._localBaseResults),function(){
|
| 148 |
},_2); |
| 149 |
} |
| 150 |
}; |
| 151 |
dojo.addOnLoad(function(){
|
| 152 |
dojo.connect(dojox.data,"restListener",function(_28){ |
| 153 |
var _29=_28.channel;
|
| 154 |
var _2a=_28.event.toLowerCase();
|
| 155 |
var _2b=dojox.rpc.JsonRest&&dojox.rpc.JsonRest.getServiceAndId(_29).service;
|
| 156 |
_21(_2a,_29,_2a=="post"?_29+_28.result.id:_29,dojo.toJson(_28.result),_2b);
|
| 157 |
}); |
| 158 |
}); |
| 159 |
var _2c=_1._change;
|
| 160 |
_1._change=function(_2d,_2e,id,_2f){ |
| 161 |
if(!_3){
|
| 162 |
return _2c.apply(this,arguments); |
| 163 |
} |
| 164 |
var _30=_2e._getRequest(id).url;
|
| 165 |
_21(_2d,_30,dojox.rpc.JsonRest._contentId,_2f,_2e); |
| 166 |
var _31=dojox.storage.get("dirty",_2)||{}; |
| 167 |
if(_2d=="put"||_2d=="delete"){ |
| 168 |
var _32=_30;
|
| 169 |
}else{
|
| 170 |
_32=0;
|
| 171 |
for(var i in _31){ |
| 172 |
if(!isNaN(parseInt(i))){
|
| 173 |
_32=i; |
| 174 |
} |
| 175 |
} |
| 176 |
_32++; |
| 177 |
} |
| 178 |
_31[_32]={method:_2d,id:_30,content:_2f};
|
| 179 |
return _f(_32,_31);
|
| 180 |
}; |
| 181 |
function _f(_33,_34){ |
| 182 |
var _35=_34[_33];
|
| 183 |
var _36=dojox.rpc.JsonRest.getServiceAndId(_35.id);
|
| 184 |
var _37=_2c(_35.method,_36.service,_36.id,_35.content);
|
| 185 |
_34[_33]=_35; |
| 186 |
dojox.storage.put("dirty",_34,function(){ |
| 187 |
},_2); |
| 188 |
_37.addBoth(function(_38){
|
| 189 |
if(_a(_38)){
|
| 190 |
return null; |
| 191 |
} |
| 192 |
var _39=dojox.storage.get("dirty",_2)||{}; |
| 193 |
delete _39[_33];
|
| 194 |
dojox.storage.put("dirty",_39,function(){ |
| 195 |
},_2); |
| 196 |
return _38;
|
| 197 |
}); |
| 198 |
return _37;
|
| 199 |
}; |
| 200 |
dojo.connect(_4,"onLoad",_5);
|
| 201 |
dojo.connect(_4,"onUpdate",_5);
|
| 202 |
})(); |
| 203 |
} |