You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Mikejo5000 <gi...@git.apache.org> on 2016/04/14 00:42:56 UTC

[GitHub] cordova-plugin-network-information pull request: added code exampl...

GitHub user Mikejo5000 opened a pull request:

    https://github.com/apache/cordova-plugin-network-information/pull/40

    added code examples

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/Mikejo5000/cordova-plugin-network-information master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/cordova-plugin-network-information/pull/40.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #40
    
----
commit 35e2d3ba8336f66b898ecf1e816282766de2fab2
Author: Mikejo5001 <m....@outlook.com>
Date:   2016-04-13T22:42:11Z

    added code examples

----


---
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


[GitHub] cordova-plugin-network-information pull request: added code exampl...

Posted by riknoll <gi...@git.apache.org>.
Github user riknoll commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-network-information/pull/40#discussion_r61010954
  
    --- 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 --
    
    Maybe just add something like `console.log("connection lost")` and forget writing to the file altogether. I think it's valuable to show the event being subscribed to.


---
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


[GitHub] cordova-plugin-network-information pull request: added code exampl...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/cordova-plugin-network-information/pull/40


---
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


[GitHub] cordova-plugin-network-information pull request: added code exampl...

Posted by Mikejo5000 <gi...@git.apache.org>.
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


[GitHub] cordova-plugin-network-information pull request: added code exampl...

Posted by riknoll <gi...@git.apache.org>.
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 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


[GitHub] cordova-plugin-network-information pull request: added code exampl...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on the pull request:

    https://github.com/apache/cordova-plugin-network-information/pull/40#issuecomment-214527826
  
    LGTM.


---
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