Skip to content

Instantly share code, notes, and snippets.

@mikebranstein
Created January 19, 2016 02:39
Show Gist options
  • Save mikebranstein/952c7b570a38d33387e6 to your computer and use it in GitHub Desktop.
Save mikebranstein/952c7b570a38d33387e6 to your computer and use it in GitHub Desktop.
How to use the offline file system module for NativeScript, assuming each function is a tap event handler. It also assumes we know the structure of the data coming back out of the file.
var offlineModule = require("./modules/offline/offline");
exports.onTapClear = function() {
console.log("clearing file");
offlineModule.remove().then(function() {
console.log("finished clearing file");
});
};
exports.onTapWrite = function() {
console.log("writing to file");
var items = [{"id": "1", "value": "bob"}, {"id": "2", "value": "tom"}];
offlineModule.write(items)
.then(function() {
console.log("wrote " + items.length + " items to offline file");
}, function(error) {
// error!
}).then(function() {
console.log("finished writing to file");
});
};
exports.onTapRead = function() {
console.log("reading from file");
var items = [];
offlineModule.read()
.then(function(content) {
console.log("read from file: " + content);
var data = JSON.parse(content);
data.forEach(function(item) {
items.push(item);
});
}, function (error) {
// error
}).then(function() {
console.log("finished reading " + items.length + " items from file");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment