Return-Path: X-Original-To: apmail-cordova-dev-archive@www.apache.org Delivered-To: apmail-cordova-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AC6E519A77 for ; Tue, 26 Apr 2016 00:02:51 +0000 (UTC) Received: (qmail 19731 invoked by uid 500); 26 Apr 2016 00:02:46 -0000 Delivered-To: apmail-cordova-dev-archive@cordova.apache.org Received: (qmail 19691 invoked by uid 500); 26 Apr 2016 00:02:46 -0000 Mailing-List: contact dev-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list dev@cordova.apache.org Received: (qmail 19679 invoked by uid 99); 26 Apr 2016 00:02:46 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Apr 2016 00:02:46 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 07D09DFE60; Tue, 26 Apr 2016 00:02:46 +0000 (UTC) From: riknoll To: dev@cordova.apache.org Reply-To: dev@cordova.apache.org References: In-Reply-To: Subject: [GitHub] cordova-plugin-network-information pull request: added code exampl... Content-Type: text/plain Message-Id: <20160426000246.07D09DFE60@git1-us-west.apache.org> Date: Tue, 26 Apr 2016 00:02:46 +0000 (UTC) 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 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