📄 sync.js
字号:
if(!dojo._hasResource["dojox.off.sync"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.dojo._hasResource["dojox.off.sync"] = true;dojo.provide("dojox.off.sync");dojo.require("dojox.storage.GearsStorageProvider");dojo.require("dojox.off._common");dojo.require("dojox.off.files");// Author: Brad Neuberg, bkn3@columbia.edu, http://codinginparadise.org// summary:// Exposes syncing functionality to offline applicationsdojo.mixin(dojox.off.sync, { // isSyncing: boolean // Whether we are in the middle of a syncing session. isSyncing: false, // cancelled: boolean // Whether we were cancelled during our last sync request or not. If // we are cancelled, then successful will be false. cancelled: false, // successful: boolean // Whether the last sync was successful or not. If false, an error // occurred. successful: true, // details: String[] // Details on the sync. If the sync was successful, this will carry // any conflict or merging messages that might be available; if the // sync was unsuccessful, this will have an error message. For both // of these, this should be an array of Strings, where each string // carries details on the sync. // Example: // dojox.off.sync.details = ["The document 'foobar' had conflicts - yours one", // "The document 'hello world' was automatically merged"]; details: [], // error: boolean // Whether an error occurred during the syncing process. error: false, // actions: dojox.off.sync.ActionLog // Our ActionLog that we store offline actions into for later // replaying when we go online actions: null, // autoSync: boolean // For advanced usage; most developers can ignore this. // Whether we do automatically sync on page load or when we go online. // If true we do, if false syncing must be manually initiated. // Defaults to true. autoSync: true, // summary: // An event handler that is called during the syncing process with // the state of syncing. It is important that you connect to this // method and respond to certain sync events, especially the // "download" event. // description: // This event handler is called during the syncing process. You can // do a dojo.connect to receive sync feedback: // // dojo.connect(dojox.off.sync, "onSync", someFunc); // // You will receive one argument, which is the type of the event // and which can have the following values. // // The most common two types that you need to care about are "download" // and "finished", especially if you are using the default // Dojo Offline UI widget that does the hard work of informing // the user through the UI about what is occuring during syncing. // // If you receive the "download" event, you should make a network call // to retrieve and store your data somehow for offline access. The // "finished" event indicates that syncing is done. An example: // // dojo.connect(dojox.off.sync, "onSync", function(type){ // if(type == "download"){ // // make a network call to download some data // // for use offline // dojo.xhrGet({ // url: "downloadData.php", // handleAs: "javascript", // error: function(err){ // dojox.off.sync.finishedDownloading(false, "Can't download data"); // }, // load: function(data){ // // store our data // dojox.storage.put("myData", data); // // // indicate we are finished downloading // dojox.off.sync.finishedDownloading(true); // } // }); // }else if(type == "finished"){ // // update UI somehow to indicate we are finished, // // such as using the download data to change the // // available data // } // }) // // Here is the full list of event types if you want to do deep // customization, such as updating your UI to display the progress // of syncing (note that the default Dojo Offline UI widget does // this for you if you choose to pull that in). Most of these // are only appropriate for advanced usage and can be safely // ignored: // // * "start" // syncing has started // * "refreshFiles" // syncing will begin refreshing // our offline file cache // * "upload" // syncing will begin uploading // any local data changes we have on the client. // This event is fired before we fire // the dojox.off.sync.actions.onReplay event for // each action to replay; use it to completely // over-ride the replaying behavior and prevent // it entirely, perhaps rolling your own sync // protocol if needed. // * "download" // syncing will begin downloading any new data that is // needed into persistent storage. Applications are required to // implement this themselves, storing the required data into // persistent local storage using Dojo Storage. // * "finished" // syncing is finished; this // will be called whether an error ocurred or not; check // dojox.off.sync.successful and dojox.off.sync.error for sync details // * "cancel" // Fired when canceling has been initiated; canceling will be // attempted, followed by the sync event "finished". onSync: function(/* String */ type){}, synchronize: function(){ /* void */ // summary: Starts synchronizing //dojo.debug("synchronize"); if(this.isSyncing || dojox.off.goingOnline || (!dojox.off.isOnline)){ return; } this.isSyncing = true; this.successful = false; this.details = []; this.cancelled = false; this.start(); }, cancel: function(){ /* void */ // summary: // Attempts to cancel this sync session if(!this.isSyncing){ return; } this.cancelled = true; if(dojox.off.files.refreshing){ dojox.off.files.abortRefresh(); } this.onSync("cancel"); }, finishedDownloading: function(successful /* boolean? */, errorMessage /* String? */){ // summary: // Applications call this method from their // after getting a "download" event in // dojox.off.sync.onSync to signal that // they are finished downloading any data // that should be available offline // successful: boolean? // Whether our downloading was successful or not. // If not present, defaults to true. // errorMessage: String? // If unsuccessful, a message explaining why if(typeof successful == "undefined"){ successful = true; } if(!successful){ this.successful = false; this.details.push(errorMessage); this.error = true; } this.finished(); }, start: function(){ /* void */ // summary: // For advanced usage; most developers can ignore this. // Called at the start of the syncing process. Advanced // developers can over-ride this method to use their // own sync mechanism to start syncing. if(this.cancelled){ this.finished(); return; } this.onSync("start"); this.refreshFiles(); }, refreshFiles: function(){ /* void */ // summary: // For advanced usage; most developers can ignore this. // Called when we are going to refresh our list // of offline files during syncing. Advanced developers // can over-ride this method to do some advanced magic related to // refreshing files. //dojo.debug("refreshFiles"); if(this.cancelled){ this.finished(); return; } this.onSync("refreshFiles"); dojox.off.files.refresh(dojo.hitch(this, function(error, errorMessages){ if(error){ this.error = true; this.successful = false; for(var i = 0; i < errorMessages.length; i++){ this.details.push(errorMessages[i]); } // even if we get an error while syncing files, // keep syncing so we can upload and download // data } this.upload(); })); }, upload: function(){ /* void */ // summary: // For advanced usage; most developers can ignore this. // Called when syncing wants to upload data. Advanced // developers can over-ride this method to completely // throw away the Action Log and replaying system // and roll their own advanced sync mechanism if needed. if(this.cancelled){ this.finished(); return; } this.onSync("upload"); // when we are done uploading start downloading dojo.connect(this.actions, "onReplayFinished", this, this.download); // replay the actions log this.actions.replay(); }, download: function(){ /* void */ // summary: // For advanced usage; most developers can ignore this. // Called when syncing wants to download data. Advanced // developers can over-ride this method to use their // own sync mechanism. if(this.cancelled){ this.finished(); return; } // apps should respond to the "download" // event to download their data; when done // they must call dojox.off.sync.finishedDownloading() this.onSync("download"); }, finished: function(){ /* void */ // summary: // For advanced usage; most developers can ignore this. // Called when syncing is finished. Advanced // developers can over-ride this method to clean // up after finishing their own sync // mechanism they might have rolled. this.isSyncing = false; this.successful = (!this.cancelled && !this.error); this.onSync("finished"); }, _save: function(callback){ this.actions._save(function(){ callback(); }); }, _load: function(callback){ this.actions._load(function(){ callback(); }); }});// summary:// A class that records actions taken by a user when they are offline,// suitable for replaying when the network reappears. // description:// The basic idea behind this method is to record user actions that would// normally have to contact a server into an action log when we are// offline, so that later when we are online we can simply replay this log// in the order user actions happened so that they can be executed against// the server, causing synchronization to happen. // // When we replay, for each of the actions that were added, we call a // method named onReplay that applications should connect to and // which will be called over and over for each of our actions -- // applications should take the offline action// information and use it to talk to a server to have this action// actually happen online, 'syncing' themselves with the server. //// For example, if the action was "update" with the item that was updated, we// might call some RESTian server API that exists for updating an item in// our application. The server could either then do sophisticated merging// and conflict resolution on the server side, for example, allowing you// to pop up a custom merge UI, or could do automatic merging or nothing// of the sort. When you are finished with this particular action, your// application is then required to call continueReplay() on the actionLog object// passed to onReplay() to continue replaying the action log, or haltReplay()// with the reason for halting to completely stop the syncing/replaying// process.//// For example, imagine that we have a web application that allows us to add// contacts. If we are offline, and we update a contact, we would add an action;// imagine that the user has to click an Update button after changing the values// for a given contact:// // dojox.off.whenOffline(dojo.byId("updateButton"), "onclick", function(evt){// // get the updated customer values// var customer = getCustomerValues();//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -