-
Notifications
You must be signed in to change notification settings - Fork 98
/
index.js
102 lines (102 loc) · 2.44 KB
/
index.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
99
100
101
102
let BaaS = window.BaaS
let BookShelf = new BaaS.TableObject('bookshelf')
let cacheKey = 'ifanrx_clientID'
new Vue({
el: '#root',
data() {
return {
bookList: [],
creatingBookName: '',
loginForm: {
email: '',
password: '',
},
registerForm: {
email: '',
password: '',
}
}
},
methods: {
createBook() {
if (!this.creatingBookName) return false
let record = BookShelf.create()
record.set({
bookName: this.creatingBookName
}).save().then(res => {
this.creatingBookName = ''
this.bookList.push({
id: res.data.id,
bookName: res.data.bookName,
disabled: true,
})
})
},
editBook(book) {
if (book.disabled) {
book.disabled = false
} else {
let record = BookShelf.getWithoutData(book.id)
record.set({bookName: book.bookName}).update().then(res => {
book.disabled = true
})
}
},
deleteBook(book) {
BookShelf.delete(book.id).then(() => {
this.bookList = this.bookList.filter(v => {
return v.id !== book.id
})
})
},
getBookList() {
BookShelf.offset(0).limit(10).find().then(res => {
res.data.objects.forEach(v => {
this.bookList.push({
id: v.id,
bookName: v.bookName,
disabled: true,
})
})
})
},
openLoginModal() {
$('#loginModal').modal()
},
openRegisterModal() {
$('#registerModal').modal()
},
handleLogin() {
BaaS.auth.login(this.loginForm).then(() => {
$('#loginModal').modal('hide')
this.init()
})
},
handleRegister() {
BaaS.auth.register(this.registerForm).then(() => {
$('#registerModal').modal('hide')
this.init()
})
},
init() {
this.getBookList()
},
},
mounted() {
if (!localStorage.getItem(cacheKey)) {
while (true) {
let clientID = window.prompt('请输入 clientID') // 从 BaaS 后台获取 ClientID
if (clientID && clientID.match(/\w{20}/)) {
localStorage.setItem(cacheKey, clientID) // 若输入了错误的 clientID,可以清空 localStorage
break
}
}
}
BaaS.init(localStorage.getItem(cacheKey))
BaaS.auth.getCurrentUser().then(() => {
this.init()
}).catch(e => {
this.openLoginModal()
})
}
})