-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_name.html
102 lines (93 loc) · 3.28 KB
/
with_name.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
<head>
<title>PouchDB</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- DOM -->
<div class="App">
<button id="sync">Sync</button>
<p id="name"></p>
</div>
<!-- Sciprts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pouchdb/7.2.2/pouchdb.min.js"></script>
<script>
let data = 'Тут может быть ваше имя'
const DBS = {
// Создаем локальную БД и коннектимся к удаленной
Local: new PouchDB('name_db'),
Remote: new PouchDB('http://admin:password@localhost:5984/name_db')
}
// Иницииируем реприкацию
const sync = DBS.Local.sync(DBS.Remote, {
live: true,
retry: true
}).on('change', ({ change }) => {
// обрабатываем изменения если в процессе синхронизации локлаьной бд таковые произошли
const doc = change.docs[0]
console.log(doc)
data = doc.name
// рисуем саму html табличку
render()
})
const render = () => {
name.innerHTML = data
}
// получение всех документов
const fetch = () => {
DBS.Local.allDocs({ include_docs: true })
.then(result => {
console.log(result)
if (result.rows[0].doc.hasOwnProperty('name')) {
data = result.rows[0].doc.name
}
render()
})
}
// Events
const name = document.getElementById('name')
const syncBtn = document.querySelector('#sync')
syncBtn.addEventListener('click', () => fetch())
render()
</script>
</body>
</html>
<script>
(function() {
var ws = new WebSocket('ws://' + window.location.host +
'/jb-server-page?reloadMode=RELOAD_ON_SAVE&'+
'referrer=' + encodeURIComponent(window.location.pathname));
ws.onmessage = function (msg) {
if (msg.data === 'reload') {
window.location.reload();
}
if (msg.data.startsWith('update-css ')) {
var messageId = msg.data.substring(11);
var links = document.getElementsByTagName('link');
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.rel !== 'stylesheet') continue;
var clonedLink = link.cloneNode(true);
var newHref = link.href.replace(/(&|\?)jbUpdateLinksId=\d+/, "$1jbUpdateLinksId=" + messageId);
if (newHref !== link.href) {
clonedLink.href = newHref;
}
else {
var indexOfQuest = newHref.indexOf('?');
if (indexOfQuest >= 0) {
// to support ?foo#hash
clonedLink.href = newHref.substring(0, indexOfQuest + 1) + 'jbUpdateLinksId=' + messageId + '&' +
newHref.substring(indexOfQuest + 1);
}
else {
clonedLink.href += '?' + 'jbUpdateLinksId=' + messageId;
}
}
link.replaceWith(clonedLink);
}
}
};
})();
</script>