forked from SwarmOnline/Ext.ux.OfflineSyncStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ext.ux.OfflineSyncStore-min.js
4 lines (4 loc) · 6.03 KB
/
Ext.ux.OfflineSyncStore-min.js
1
2
3
4
/*
* Ext.ux.OfflineSyncStore
*/
Ext.define("Ext.ux.OfflineSyncStore",{extend:"Ext.data.Store",config:{trackLocalSync:true,autoServerSync:true,localProxy:null,serverProxy:null},statics:{CREATED:"created",UPDATED:"updated",REMOVED:"removed"},constructor:function(a){a=a||{};this.callParent([a])},loadLocal:function(a,b){this._proxy=this.getLocalProxy();this.load(a,b,true)},loadServer:function(a,b){this._proxy=this.getServerProxy();this.on({load:{fn:this.onServerLoad,single:true,scope:this}});this.load(a,b,true)},sync:function(){this._proxy=this.getLocalProxy();var d=this.callParent(arguments);var c=d.added,a=d.updated,b=d.removed;if(this.getTrackLocalSync()){if(c.length>0){this.storeCreated(c)}if(a.length>0){this.storeUpdated(a)}if(b.length>0){this.storeRemoved(b)}}return d},onBatchComplete:function(a){this.callParent(arguments);if(this.isLocalMode()){this.fireEvent("localsynccomplete",this,a);if(this.doAutoServerSync()){this.syncServer()}}else{this.resetPhantomForCreatedRecords(a);this.fireEvent("remotesynccomplete",this,a)}},onBatchException:function(b,a){this.callParent(arguments);if(this.isLocalMode()){this.fireEvent("localsyncexception",this,b,a)}else{this.resetPhantomForCreatedRecords(b);this.fireEvent("remotesyncexception",this,b,a)}},resetPhantomForCreatedRecords:function(a){Ext.each(a.operations,function(b){if(b.getAction()==="create"){Ext.each(b.getRecords(),function(c){c.phantom=false},this)}},this)},syncServer:function(){this._proxy=this.getServerProxy();var f=this,c={},g=f.getModifiedRecordsCollection(Ext.ux.OfflineSyncStore.CREATED),e=f.getModifiedRecordsCollection(Ext.ux.OfflineSyncStore.UPDATED),b=f.getRemovedRecordsCollection(Ext.ux.OfflineSyncStore.REMOVED),h=false;if(g.length>0){for(var d=0;d<g.length;d++){var a=g[d];a.data[this.getModel().getIdProperty()]=a.id;a.phantom=true}c.create=g;h=true}if(e.length>0){c.update=e;h=true}if(b.length>0){c.destroy=b;h=true}if(h&&f.fireEvent("beforesync",this,c)!==false){f.getProxy().batch({operations:c,listeners:f.getBatchListeners()})}return{added:g,updated:e,removed:b}},storeCreated:function(a){this.storeChanged(a,Ext.ux.OfflineSyncStore.CREATED,false)},storeUpdated:function(d){var g=this.getModifiedCollection(Ext.ux.OfflineSyncStore.CREATED),e=[],k=[],a=false,h=this.getModel().getIdProperty();for(var c=0;c<d.length;c++){var f=d[c];for(var b=0;b<g.length;b++){if(f.data[h]===g[b][h]){a=true;break}}if(a){e.push(f.data)}else{k.push(f)}}this.storeChanged(this.mergeOrReplaceArrays(g,e),Ext.ux.OfflineSyncStore.CREATED,true);this.storeChanged(k,Ext.ux.OfflineSyncStore.UPDATED,false)},storeRemoved:function(f){var g=this.getModifiedCollection(Ext.ux.OfflineSyncStore.CREATED),e=this.getModifiedCollection(Ext.ux.OfflineSyncStore.UPDATED),l=[],m=false,h=this.getModel().getIdProperty();for(var d=0;d<f.length;d++){var c=f[d];for(var b=0;b<g.length;b++){if(c.data[h]===g[b][h]){m=true;g.splice(b,1);break}}for(var a=0;a<e.length;a++){if(c.data[h]===e[a][h]){e.splice(a,1);break}}if(!m){l.push(c)}}this.storeChanged(g,Ext.ux.OfflineSyncStore.CREATED,true);this.storeChanged(e,Ext.ux.OfflineSyncStore.UPDATED,true);this.storeChanged(l,Ext.ux.OfflineSyncStore.REMOVED,false)},storeChanged:function(e,j,a){var f=this.getLocalProxy().getId()+"-"+j,g=[];for(var c=0;c<e.length;c++){var b=e[c].isModel?e[c].data:e[c];g.push(b)}var h=localStorage.getItem(f),d=!Ext.isEmpty(h)?Ext.decode(h):[];if(!a){g=this.mergeOrReplaceArrays(d,g)}localStorage.removeItem(f);localStorage.setItem(f,Ext.encode(g))},onCreateRecords:function(b,a,c){this.callParent(arguments);if(c&&!this.isLocalMode()){this.clearModifiedCollection(Ext.ux.OfflineSyncStore.CREATED)}},onUpdateRecords:function(b,a,c){this.callParent(arguments);if(c&&!this.isLocalMode()){this.clearModifiedCollection(Ext.ux.OfflineSyncStore.UPDATED)}},onDestroyRecords:function(b,a,c){this.callParent(arguments);if(c&&!this.isLocalMode()){this.clearModifiedCollection(Ext.ux.OfflineSyncStore.REMOVED)}},clearModifiedCollection:function(a){localStorage.removeItem(this.getLocalProxy().getId()+"-"+a)},clearCollections:function(){var a=Ext.ux.OfflineSyncStore;var b=[a.REMOVED,a.UPDATED,a.CREATED];b.forEach(function(c){this.clearModifiedCollection(c)},this)},getModifiedCollection:function(b){var c=localStorage.getItem(this.getLocalProxy().getId()+"-"+b),a=!Ext.isEmpty(c)?Ext.decode(c):[];return a},getModifiedRecordsCollection:function(d){var e=this.getModifiedCollection(d),b=[];for(var c=0;c<e.length;c++){var a=this.getById(e[c][this.getModel().getIdProperty()]);if(a){b.push(a)}}return b},getRemovedRecordsCollection:function(d){var e=this.getModifiedCollection(d),b=[];for(var c=0;c<e.length;c++){var a=this.getModel().create(e[c]);if(a){b.push(a)}}return b},onServerLoad:function(b,a,d){if(d){this._proxy=this.getLocalProxy();this._proxy.clear();b.each(function(e){e.setDirty(true)});var c=this.getTrackLocalSync();if(c){this.disableTrackLocalSync()}this.sync();b.each(function(e){e.commit()});if(c){this.enableTrackLocalSync()}}else{this.loadLocal()}},isLocalMode:function(){return this.getProxy()===this.getLocalProxy()},disableTrackLocalSync:function(){this.setTrackLocalSync(false)},enableTrackLocalSync:function(){this.setTrackLocalSync(true)},hasPendingServerSync:function(){return this.hasPendingCreated()||this.hasPendingUpdated()||this.hasPendingRemoved()},hasPendingCreated:function(){return this.getModifiedCollection(Ext.ux.OfflineSyncStore.CREATED).length>0},hasPendingUpdated:function(){return this.getModifiedCollection(Ext.ux.OfflineSyncStore.UPDATED).length>0},hasPendingRemoved:function(){return this.getModifiedCollection(Ext.ux.OfflineSyncStore.REMOVED).length>0},doAutoServerSync:function(){var a=this.getAutoServerSync();if(Ext.isFunction(a)){a=a.call(this)}return a},applyServerProxy:function(b,a){return this.applyProxy(b,a)},updateServerProxy:function(a){this.updateProxy(a)},applyLocalProxy:function(b,a){return this.applyProxy(b,a)},updateLocalProxy:function(a){this.updateProxy(a)},mergeOrReplaceArrays:function(g,f,b){var e=0,c=g.length,a=f.length,h=false;b=b||this.getModel().getIdProperty();for(;e<c;e++){for(var d=0;d<a;d++){if(g[e][b]===f[d][b]){h=true;break}}if(!h){f.push(g[e])}h=false}return f}});