-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
96 lines (84 loc) · 2.3 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
/**
* Haskell Blog
*/
var Blog = {};
Blog.loadEntries = function () {
$.get("/news", function (n) {
var html = BlogUI.news({"news": n});
$('#main').html(html);
});
};
Blog.addEntry = function (author, title, content, tags) {
$.ajax({
type: "POST",
url: "/news",
data: JSON.stringify({
author: author,
title: title,
content: content,
tags: tags
}),
success: function () {
Blog.resetAddForm();
Blog.loadEntries();
},
error: function () {
alert("Beitrag konnte nicht angelegt werden");
}
});
};
Blog.storeComment = function (newsId, author, text, onOk) {
$.ajax({
type: "POST",
url: "/comments",
data: JSON.stringify({
author: author,
comment: text,
news: "" + newsId
}),
success: onOk,
error: function () {
alert("Kommentar konnte nicht gespeichert werden");
}
});
};
Blog.resetAddForm = function () {
$('#author').val(""); $('#title').val(""); $('#content').val(""); $('#tags').val("");
};
Blog.showComments = function (id) {
$.get("/comments/" + id, function (c) {
var html = BlogUI.comments({"comments": c});
$('#commentsFor' + id).html(html).slideDown();
});
};
Blog.showCommentsClick = function (id) {
var el = $('#commentLink' + id);
if (el.hasClass('commentsClosed')) {
Blog.showComments(id);
el.text("Kommentare verbergen");
el.removeClass('commentsClosed');
} else {
el.text("Kommentare anzeigen");
$('#commentsFor' + id).slideUp();
el.addClass('commentsClosed');
}
};
Blog.addComment = function (id) {
var authorEl = $('#commentAuthor' + id);
var textEl = $('#commentText' + id);
Blog.storeComment(id, authorEl.val(), textEl.val(), function () {
authorEl.val(""); textEl.val("");
Blog.showComments(id);
});
};
$(function () {
Blog.loadEntries();
$('#newNews').submit(function (e) {
e.preventDefault();
Blog.addEntry($('#author').val(),
$('#title').val(),
$('#content').val(),
$('#tags').val().split(",")
);
});
});