forked from The-Safe-Place-App/main-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (97 loc) · 3.81 KB
/
app.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// connect to your Firebase application using your reference URL
var messageAppReference = new Firebase("https://js-dev-project-leon.firebaseio.com/");
$(document).ready(function() {
$('#message-form').submit(function (event) {
// by default a form submit reloads the DOM which will subsequently reload all our JS
// to avoid this we preventDefault()
event.preventDefault()
// grab user message input
// conditional for empty input field use case
var message = $('#message').val()
if (message !== ""){
// grab user message input
var message = $('#message').val()
// clear message input (for UX purposes)
$('#message').val('')
// create a section for messages data in your db
var messagesReference = messageAppReference.child('anonymous');
// use the set method to save data to the messages
messagesReference.push({
message: message,
votes: 0
})
}
else{
$('span').text("type something in order to post!");
}
})
// // on initialization of app (when document is ready) get fan messages
messageClass.postAnon();
});
var messageClass = (function () {
function postAnon() {
// retrieve messages data when .on() initially executes
// and when its data updates
messageAppReference.child('anonymous').on('value', function (results) {
var $messageBoard = $('.message-board')
var messages = []
var allMessages = results.val();
// iterate through results coming from database call; messages
for (var msg in allMessages) {
// get method is supposed to represent HTTP GET method
var message = allMessages[msg].message
var votes = allMessages[msg].votes
// create message element
var $messageListElement = $('<li></li>')
var $messageListElement = $('<li class="anonymousPost"></li>')
// create up vote element
var $upVoteElement = $('<i class="fa fa-thumbs-up pull-right"></i>')
$upVoteElement.on('click', function (e) {
var id = $(e.target.parentNode).data('id')
updateMessage(id, ++votes)
})
// create down vote element
var $downVoteElement = $("<i class='fa fa-thumbs-o-down pull-right' aria-hidden='true'></i>")
var $downVoteElement = $('<i class="fa fa-thumbs-down pull-right"></i>')
$downVoteElement.on('click', function (e) {
var id = $(e.target.parentNode).data('id')
updateMessage(id, --votes)
})
// add id as data attribute so we can refer to later for updating
$messageListElement.attr('data-id', msg)
// add message to li
$messageListElement.html(message)
// add voting elements
$messageListElement.append($upVoteElement)
$messageListElement.append($downVoteElement)
// show votes
$messageListElement.append('<div class="pull-right">' + votes + '</div>')
// push element to array of messages
messages.push($messageListElement)
// remove lis to avoid dupes
$messageBoard.empty()
for (var i in messages) {
$messageBoard.append(messages[i])
}
}
})
}
function updateMessage(id, votes) {
// find message whose objectId is equal to the id we're searching with
var messageReference = new Firebase('https://js-dev-project-leon.firebaseio.com/anonymous/' + id)
// update votes property
messageReference.update({
votes: votes
})
}
function deleteMessage(id) {
// find message whose objectId is equal to the id we're searching with
var messageReference = new Firebase('https://js-dev-project-leon.firebaseio.com/anonymous/' + id)
messageReference.remove();
$("li").attr("data-id",id).remove();
messageClass.postAnon();
}
return {
postAnon: postAnon
}
})();