-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaveAMessage.js
74 lines (65 loc) · 2.29 KB
/
leaveAMessage.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
!function(){
var view = document.querySelector('section.messageList')
var controller = {
view:null,
messageList:null,
init:function(view){
this.view = view
this.messageList = view.querySelector('#messageList')
this.form= view.querySelector('form')
this.initAV()
this.loadMessages()
this.bindEvents()
},
initAV:function(){
var APP_ID = '7pp9hNxP5KSSuq5RMAFc2Fcq-gzGzoHsz';
var APP_KEY = 'POxVDL86RVWQm7OHPJFjAmcv';
AV.init({ appId: APP_ID, appKey: APP_KEY });
},
loadMessages:function(){
var query = new AV.Query('LeavingAMessage');
query.find().then(function (Messages) {
let arr = Messages.map((item) => item['attributes'])
arr.forEach((item)=>{
let li = document.createElement('li')
li.innerText = `${item['name']}:${item['content']}`
let messageList = document.querySelector('#messageList')
messageList.append(li)
})
}, function (error) {
alert('请稍后再试试,试试就逝世')
// 异常处理
})
},
bindEvents:function(){
let myForm = this.form
this.form.addEventListener('submit',function(e){
e.preventDefault()//阻止默认(刷新)事件
let content = myForm.querySelector('input[name=content]').value//获取input中的值
let name = myForm.querySelector('input[name=name]').value
var LeavingAMessage = AV.Object.extend('LeavingAMessage');
var message = new LeavingAMessage()
message.save({
'name':name,
'content':content
}).then((object) => {
let li = document.createElement('li')
li.innerText = `${object.attributes['name']}:${object.attributes['content']}`
let messageList = document.querySelector('#messageList')
myForm.querySelector('input[name=content]').value = ''
messageList.append(li)
})
})
},
saveMessage:function(){
}
}
controller.init(view)
// Test-Code
// var TestObject = AV.Object.extend('TestObject');
// var testObject = new TestObject();
// testObject.save({
// words: 'Hello World!'
// }).then(function(object) {
// })
}.call()