-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget-json-file.js
27 lines (22 loc) · 908 Bytes
/
get-json-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Download HTTP content using the fetch API
// See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
const {Button, TextView, ui} = require('tabris');
new Button({
left: 16, right: 16, top: 'prev() 12',
text: 'load'
}).on('select', loadData).appendTo(ui.contentView);
let textView = new TextView({
left: 16, right: 16, top: 'prev() 12'
}).appendTo(ui.contentView);
function loadData() {
fetch('https://raw.githubusercontent.com/mrmccormack/imd-learning-tabris/master/hello.json')
.then(response => checkStatus(response) && response.json())
.then(json => textView.text = `You appear to be in ${json.city ? json.city : json.country_name}`)
.catch(err => console.error(err)); // Never forget the final catch!
}
function checkStatus(response) {
if (!response.ok) {
throw new Error(`HTTP ${response.status} - ${response.statusText}`);
}
return response;
}