forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
98 lines (91 loc) · 2.66 KB
/
main.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
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
/**
* WARNING: no transpile
* use old plain javascript only!
*/
var listBox = document.querySelector('#list-box');
var insertBox = document.querySelector('#insert-box');
var heroesList = document.querySelector('#heroes-list');
var leaderIcon = document.querySelector('#leader-icon');
var heroSchema = {
"title": "hero schema",
"description": "describes a simple hero",
"primaryKey": "name",
"version": 0,
"type": "object",
"properties": {
"name": {
"type": "string"
},
"color": {
"type": "string"
}
},
"required": ["color"]
};
console.log('hostname: ' + window.location.hostname);
const syncURL = 'http://' + window.location.hostname + ':10102/';
window.RxDB
.createRxDatabase({
name: 'heroesdb',
storage: RxDB.getRxStoragePouch('idb'),
password: 'myLongAndStupidPassword'
})
.then(function(db) {
console.log('created database');
window.db = db;
heroesList.innerHTML = 'Create collection..';
db.waitForLeadership().then(function() {
document.title = '♛ ' + document.title;
leaderIcon.style.display = 'block';
});
return db.addCollections({
hero: {
schema: heroSchema
}
});
})
.then(function() {
var col = window.db.hero;
window.col = col;
return col;
})
.then(function(col) {
console.log('DatabaseService: sync');
col.syncCouchDB({
remote: syncURL + 'hero/'
});
return col;
})
.then(function(col) {
col.find()
.sort({
name: 1
})
.$.subscribe(function(heroes) {
if (!heroes) {
heroesList.innerHTML = 'Loading..';
return;
}
console.log('observable fired');
console.dir(heroes);
heroesList.innerHTML = '';
heroes.forEach(function(hero) {
heroesList.innerHTML = heroesList.innerHTML +
'<li>' +
'<div class="color-box" style="background:' + hero.color + '"></div>' +
'<div class="name">' + hero.name + '</div>' +
'</li>';
});
});
});
addHero = function() {
var name = document.querySelector('input[name="name"]').value;
var color = document.querySelector('input[name="color"]').value;
var obj = {
name: name,
color: color
};
console.log('inserting hero:');
console.dir(obj);
col.insert(obj);
}