Last active
July 25, 2018 18:51
-
-
Save mrmccormack/769eadb17687910c9257342c45e225ae to your computer and use it in GitHub Desktop.
Read write JSON - Tabris.js
This file contains 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
const {Button, fs, TextInput, ui} = require('tabris') | |
const FILE = fs.filesDir + '/' + 'names.json' | |
const RIGHT_MARGIN = 10 | |
const LEFT_MARGIN = 10 | |
let names = [ | |
{ | |
'firstName': 'Bob', | |
'lastName': 'Jones' | |
}, | |
{ | |
'firstName': 'Jill', | |
'lastName': 'Willard' | |
} | |
] | |
console.log(JSON.stringify(names)) | |
names.forEach((name, index) => { | |
let txiFirstName = new TextInput({ | |
id: 'txiFirstName' + (index), | |
top: 'prev() 10', | |
text: name.firstName | |
}).appendTo(ui.contentView) | |
txiFirstName.on('textChanged', () => { | |
txiFirstName.borderColor = '#0f0' | |
name.firstName = txiFirstName.text | |
console.log('textChanged: \nname.firstName = ' + name.firstName) | |
console.log(JSON.stringify(names)) | |
}) | |
let txiLastName = new TextInput({ | |
id: 'txiLastName' + (index), | |
top: 'prev() 10', | |
text: name.lastName | |
}).appendTo(ui.contentView) | |
txiLastName.on('textChanged', () => { | |
name.lastName = txiLastName.text | |
}) | |
}) // end forEach | |
let btnReadFile = new Button({ | |
centerX: 0, top: 'prev() 10', width: 200, | |
text: 'Read File: ' + FILE | |
}).appendTo(ui.contentView) | |
btnReadFile.on('select', () => { | |
readFile(FILE. names) | |
console.log(JSON.stringify(names)) | |
}) | |
let btnWriteFile = new Button({ | |
centerX: 0, top: 'prev() 10', width: 200, | |
text: 'Write File: ' + FILE | |
}).appendTo(ui.contentView) | |
let btnRemoveFile = new Button({ | |
centerX: 0, top: 'prev() 10', width: 200, | |
text: 'Remove File: ' + FILE | |
}).appendTo(ui.contentView) | |
btnWriteFile.on('select', () => { | |
let jnames = JSON.stringify(names) | |
fs.writeFile(FILE, jnames, 'utf-8') | |
.then(() => { | |
console.log('file written:', FILE) | |
txiFile.text = jnames | |
console.log(JSON.stringify(names)) | |
} | |
) | |
.catch(err => console.error(err)) | |
}) | |
btnRemoveFile.on('select', () => { | |
fs.removeFile(FILE) | |
.then(() => console.log('file removed:', FILE)) | |
.catch(err => console.error(err)) | |
}) | |
let txiFile = new TextInput({ | |
top: 'prev() 20', left: 5, right: 5, | |
type: 'multiline' | |
}).appendTo(ui.contentView) | |
function readFile (filename, names) { | |
fs.readFile(FILE, 'utf-8') | |
.then(text => txiFile.text = text) | |
.then(text => { | |
names = JSON.parse(text) | |
console.log(JSON.stringify(names)) | |
readNames(names) | |
}) | |
.catch(err => console.error(err)) | |
} | |
function readNames (names) { | |
names.forEach((name, index) => { | |
let t = '#txiFirstName' + index | |
ui.contentView.apply({[t]: {text: name.firstName}}) | |
console.log(JSON.stringify(names)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment