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