cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mikejo5000 <...@git.apache.org>
Subject [GitHub] cordova-plugin-network-information pull request: added code exampl...
Date Mon, 25 Apr 2016 22:18:19 GMT
Github user Mikejo5000 commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-network-information/pull/40#discussion_r60999738
  
    --- 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 --
    
    Yes, that is what I had in mind for offlineWrite; some in-memory data of some kind gets
written when you go offline. But yes, really you could write to the file anytime so this does
seem too contrived... I could add a timestamp to the file when you go offline (following your
suggestion), either that or maybe it's best that I remove the offline event handler completely
for this scenario. Any preference or other suggestion?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message