-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
66 lines (50 loc) · 1.75 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
/**
* @class Angular controller for notes list.
* @param {angularScope} $scope
*/
var remoteDbUrl = 'http://localhost:5984/notes',
NotesController = function ($scope) {
var notes = $scope.notes = [],
onDbError = function (err) {
console.error(err);
},
onDbGetAll = function (result) {
$scope.loadNotes(result.rows);
}
onDbCreated = function (db) {
dbHelper.getAll().then(onDbGetAll, onDbError);
},
onDbGet = function (doc) {
$scope.$apply(function () {
$scope.notes.push(doc);
});
},
// I would love to use angularjs $q, however it sucks
// so I'm using q.js instead.
dbHelper = new DB(window.PouchDB, Q, _, remoteDbUrl);
dbHelper.createDB('notes').then(onDbCreated, onDbError);
$scope.onDbChange = function (change) {
console.log(change);
};
dbHelper.onChange($scope.onDbChange);
$scope.loadNotes = function (notes) {
var i;
for (i = 0; i < (notes.length - 1); i += 1) {
var note = notes[i];
dbHelper.get(note.id).then(onDbGet, onDbError);
};
};
$scope.addNote = function () {
var newNote = $scope.newNote.trim(),
noteData = {
_id: new Date().toISOString(),
body: newNote
};
notes.push(noteData);
dbHelper.put(noteData);
};
$scope.removeNote = function (note) {
notes.splice(notes.indexOf(note), 1);
dbHelper.remove(note);
};
};