Github user riknoll commented on a diff in the pull request:

    
https://github.com/apache/cordova-plugin-network-information/pull/40#discussion_r60995346
  
    --- Diff: README.md ---
    @@ -210,4 +210,120 @@ When running in the Emulator, the `connection.status` 
is always unknown, so this
     
     The Emulator reports the connection type as `Cellular`, which does not 
change, so events does _not_ fire.
     
    +## Sample: Upload a File Depending on your Network State
    +
    +The code examples in this section show examples of changing app behavior 
using the online and offline events and your network connection status.
    +
    +To start with, create a new FileEntry object (data.txt) to use for sample 
data. Call this function from the `deviceready` handler.
    +
    +>*Note* This code example requires the File plugin.
    +
    +```js
    +
    +var dataFileEntry;
    +
    +function createSomeData() {
    +
    +    window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function 
(fs) {
    +
    +        console.log('file system open: ' + fs.name);
    +        // Creates a new file or returns an existing file.
    +        fs.root.getFile("data.txt", { create: true, exclusive: false }, 
function (fileEntry) {
    +
    +          dataFileEntry = fileEntry;
    +
    +        }, onErrorCreateFile);
    +
    +    }, onErrorLoadFs);
    +}
    +```
    +
    +Next, add listeners for the online and offline events in the `deviceready` 
handler.
    +
    +```js
    +document.addEventListener("offline", onOffline, false);
    +document.addEventListener("online", onOnline, false);
    +```
    +
    +The app's `onOnline` function handles the online event. In the event 
handler, check the current network state. In this app, treat any connection 
type as good except Connection.NONE. If you have a connection, you try to 
upload a file.
    +
    +```js
    +function onOnline() {
    +    // Handle the online event
    +    var networkState = navigator.connection.type;
    +
    +    if (networkState !== Connection.NONE) {
    +        if (dataFileEntry) {
    +            tryToUploadFile();
    +        }
    +    }
    +    display('Connection type: ' + networkState);
    +}
    +```
    +
    +When the online event fires in the preceding code, call the app's 
`tryToUploadFile` function.
    +
    +If the FileTransfer object's upload function fails, call the app's 
`offlineWrite` function to save the current data somewhere.
    +
    +>*Note* This example requires the FileTransfer plugin.
    +
    +```js
    +function tryToUploadFile() {
    +    // !! Assumes variable fileURL contains a valid URL to a text file on 
the device,
    +    var fileURL = getDataFileEntry().toURL();
    +
    +    var success = function (r) {
    +        console.log("Response = " + r.response);
    +        display("Uploaded. Response: " + r.response);
    +    }
    +
    +    var fail = function (error) {
    +        console.log("An error has occurred: Code = " + error.code);
    +        offlineWrite("Failed to upload: some offline data");
    +    }
    +
    +    var options = new FileUploadOptions();
    +    options.fileKey = "file";
    +    options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
    +    options.mimeType = "text/plain";
    +
    +    var ft = new FileTransfer();
    +    // Make sure you add the domain of your server URL to the
    +    // Content-Security-Policy <meta> element in index.html.
    +    ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
    +};
    +```
    +
    +In addition to calling `offlineWrite` from the error handler for the 
upload function, you also call the same `offlineWrite` function from the app's 
offline event handler.
    +
    +```js
    +function onOffline() {
    --- End diff --
    
    It would be nice to clarify why you would want to write data when going 
offline. It looks like the purpose of the `offlineWrite` function is to write 
data generated by the app to some local location so that it can be uploaded as 
soon as you go online. Do you need to use the offline event at all in this 
particular scenario? It seems like the existence of that temp file would be 
enough to indicate that you were offline at some point (though I guess it 
wouldn't show when you went offline).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org

Reply via email to