Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Last active November 22, 2019 14:31
Show Gist options
  • Save aaronmcadam/45660d5a9ec378c0a5d7a62220c3733b to your computer and use it in GitHub Desktop.
Save aaronmcadam/45660d5a9ec378c0a5d7a62220c3733b to your computer and use it in GitHub Desktop.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var errorHandler = function(fileName, e) {
var msg = "";
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = "Storage quota exceeded";
break;
case FileError.NOT_FOUND_ERR:
msg = "File not found";
break;
case FileError.SECURITY_ERR:
msg = "Security error";
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = "Invalid modification";
break;
case FileError.INVALID_STATE_ERR:
msg = "Invalid state";
break;
default:
msg = "Unknown error";
break;
}
console.log("Error (" + fileName + "): " + msg);
};
var app = {
// Application Constructor
initialize: function() {
document.addEventListener(
"deviceready",
this.onDeviceReady.bind(this),
false
);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent("deviceready");
console.log(cordova.file);
function writeToFile(fileName, data) {
data = JSON.stringify(data, null, "\t");
window.resolveLocalFileSystemURL(
cordova.file.dataDirectory,
function(directoryEntry) {
directoryEntry.getFile(
fileName,
{ create: true },
function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
// for real-world usage, you might consider passing a success callback
console.log('Write of file "' + fileName + '"" completed.');
};
fileWriter.onerror = function(e) {
// you could hook this up with our global error handler, or pass in an error callback
console.log("Write failed: " + e.toString());
};
var blob = new Blob([data], { type: "text/plain" });
fileWriter.write(blob);
}, errorHandler.bind(null, fileName));
},
errorHandler.bind(null, fileName)
);
},
errorHandler.bind(null, fileName)
);
}
function readFromFile(fileName, cb) {
var pathToFile = cordova.file.dataDirectory + fileName;
window.resolveLocalFileSystemURL(
pathToFile,
function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
cb(JSON.parse(this.result));
};
reader.readAsText(file);
}, errorHandler.bind(null, fileName));
},
errorHandler.bind(null, fileName)
);
}
writeToFile("example.json", { foo: "bar" });
readFromFile("example.json", function(data) {
console.log("data", JSON.stringify(data));
});
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector(".listening");
var receivedElement = parentElement.querySelector(".received");
listeningElement.setAttribute("style", "display:none;");
receivedElement.setAttribute("style", "display:block;");
console.log("Received Event: " + id);
}
};
app.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment