-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
130 lines (108 loc) · 3.73 KB
/
Copy pathscript.js
File metadata and controls
130 lines (108 loc) · 3.73 KB
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
function showError(error) {
var message = "An error occured";
if (error.message) {
message = error.message;
} else if (error.errors) {
var errors = error.errors;
message = "";
Object.keys(errors).forEach(function(k) {
message += k + ": " + errors[k] + "\n";
});
}
alert(message);
}
$(document).ready(function() {
var currentUser = null;
checkLogin();
loadComments();
$('#refresh-btn').click(loadComments);
$('#comment-form').submit(function() {
//Get the data from the form
var comment = $('#comment').val();
dpd.comments.post({
comment: comment
}, function(comment, error) {
if (error) return showError(error);
addComment(comment);
$('#comment').val('');
});
return false;
});
$('#login-form').submit(function() {
var login = {
email: $('#email').val(),
password: $('#password').val()
};
dpd.users.login(login, function(result, error) {
if (error) return showError(error);
showUser(result.user);
});
return false;
});
$('#logout-btn').click(function() {
dpd.users.logout(function(success, error) {
if (error) return showError(error);
currentUser = null;
$('#login-form').show();
$('#greeting').hide();
});
return false;
});
function addComment(comment) {
var editLink = $('<a href="#">Edit</a>');
var deleteLink = $('<a href="#">Delete</a>');
var div = $('<div class="comment">')
.append($('<div class="links">').append(editLink).append(deleteLink))
.append('<div class="author">Posted by: ' + comment.name + '</div>')
.append('<p>' + comment.comment + '</p>')
.appendTo('#comments');
if (comment.age && comment.age < 100) {
div.append('<div class="author">' + comment.age.toFixed(0) + ' seconds ago</div>');
} else {
var date = new Date(comment.timestamp).toLocaleDateString();
div.append('<div class="author">on ' + date + '</div>');
}
deleteLink.click(function() {
dpd.comments.del(comment._id, function(success, error) {
if (error) return showError(error);
if (success) div.remove();
});
return false;
});
editLink.click(function() {
var newComment = prompt("Enter the new comment text:", comment.comment);
if (newComment) {
dpd.comments.put(comment._id, {comment: newComment}, function(result, error) {
if (error) { return showError(error); }
comment = result;
div.find('p').text(comment.comment);
});
return false;
}
});
}
function loadComments() {
dpd.comments.get(function(result, error) { //Use the Deployd SDK to send a request to the server
$('#comments').empty(); //Empty the list
result.forEach(function(comment) { //Loop through the result
addComment(comment); //Add it to the DOM.
});
});
}
function checkLogin() {
dpd.users.me(function(user, error) {
if (user) {
showUser(user);
} else {
currentUser = null;
$('#login-form').show();
$('#greeting').hide();
}
});
}
function showUser(user) {
currentUser = user;
$('#login-form').hide();
$('#greeting').show().find('h3').text("Welcome, " + user.name);
}
});