forked from ddprrt/fh-offlineapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
55 lines (48 loc) · 1.17 KB
/
script.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var stuff = { entries: []};
var $data = $('#data');
var $newentry = $('#newentry');
var $message = $('#message');
var $window = $(window);
var dataToSave = false;
function append(d) {
$data.append('<li><input type="checkbox" value="'+d.checked+'"<span>' + d.title + '</span></li>');
}
function showStuff() {
$data.html('');
for(var i = 0; i < stuff.entries.length; i++) {
append(stuff.entries[i]);
}
}
function saveStuff() {
window.localStorage.setItem("stuff", JSON.stringify(stuff));
$.post('/api', JSON.stringify(stuff), function(data) {
stuff = data;
showStuff();
dataToSave = false;
});
}
$window.on('online', function() {
if(dataToSave) {
saveStuff();
}
});
if(!navigator.onLine) {
$message.text("Sie sind gerade offline.");
stuff = JSON.parse(window.localStorage.getItem("stuff"));
showStuff();
}
$.get('/api', function(data) {
stuff = data;
showStuff();
});
$('#submit').on('click', function(e) {
e.preventDefault();
stuff.entries.push($newentry.val());
if(navigator.onLine) {
saveStuff();
} else {
$message.text("Aber ich bin doch offline! Speichern wir später");
dataToSave = true;
append('<li class="tosave">' + $newentry.val() + "</li>");
}
});