forked from CodeDraken/electron-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStore.js
43 lines (31 loc) · 818 Bytes
/
DataStore.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
'use strict'
const Store = require('electron-store')
class DataStore extends Store {
constructor (settings) {
super(settings)
// initialize with todos or empty array
this.todos = this.get('todos') || []
}
saveTodos () {
// save todos to JSON file
this.set('todos', this.todos)
// returning 'this' allows method chaining
return this
}
getTodos () {
// set object's todos to todos in JSON file
this.todos = this.get('todos') || []
return this
}
addTodo (todo) {
// merge the existing todos with the new todo
this.todos = [ ...this.todos, todo ]
return this.saveTodos()
}
deleteTodo (todo) {
// filter out the target todo
this.todos = this.todos.filter(t => t !== todo)
return this.saveTodos()
}
}
module.exports = DataStore