Revision 613961333362 () - Diff

Link to this snippet: https://friendpaste.com/1Am5LLeuTrbDugOyW9jf5s
Embed:
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
<!DOCTYPE html>
<html>
<head><title>Tiny CouchApp</title></head>
<body>
<h1>Tiny CouchApp</h1>
<form id="new_message">
<label for="message">Message:</label>
<input type="text" name="message" value="">
<p><input type="submit" value="Save &rarr;"></p>
</form>
<ul id="messages"></ul>
</body>
<script src="/_utils/script/jquery.js"></script>
<script src="/_utils/script/jquery.couch.js"></script>
<script>
$(function() {
var db = $.couch.db("mydb");
$("#new_message").submit(function() {
// save the message to couchdb
var doc = {}, input = $("input[name=message]", this);
doc.message = input.val();
doc.created_at = new Date();
console.log(doc);
db.saveDoc(doc, {
success : function(r) {
input.val("");
}
});
return false;
});
function redrawMessages() {
db.view("messages/by_time", {
descending : true,
success : function(resp) {
var list = $("#messages");
list.empty();
resp.rows.forEach(function(row) {
list.append('<li>'+row.value+'</li>');
});
}
})
};
redrawMessages();
var changeHandler = db.changes();
changeHandler.onChange(redrawMessages);
});
</script>
</html>