Created
January 19, 2016 02:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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