Permalink
Browse files

Adding comments to the ajax load

  • Loading branch information...
1 parent 9be968d commit cee4455e11d730a6babe534995a9e5fa4795b7fd @DFectuoso DFectuoso committed Apr 4, 2010
Showing with 18 additions and 4 deletions.
  1. +10 −3 main.py
  2. +8 −1 templates/main.html
View
13 main.py
@@ -66,18 +66,25 @@ def user_fullname(self):
return fullname(username(self.user))
#Json representations
+def comment_dict(comment):
+ return {
+ 'user': comment.user_fullname(),
+ 'body': comment.body,
+ 'created': str(comment.created)}
+
def updates_dict(update):
return {
'user_fullname':update.user_fullname(),
'body':update.body,
- 'created':str(update.created)}
+ 'created':str(update.created),
+ 'comments':map(lambda c: comment_dict(c), update.comment_set) }
# Handlers:
class UpdatesHandler(webapp.RequestHandler):
def get(self,cursor):
updates_query = Update.all().order('-created')
- foo = updates_query.with_cursor(urllib.unquote(cursor)).fetch(UPDATES_LIMIT)
- self.response.out.write(simplejson.dumps([{'messages':map((lambda bar: updates_dict(bar)), foo)}, {'cursor':updates_query.cursor()}]))
+ updates_with_cursor = updates_query.with_cursor(urllib.unquote(cursor)).fetch(UPDATES_LIMIT)
+ self.response.out.write(simplejson.dumps([{'messages':map((lambda u: updates_dict(u)), updates_with_cursor)}, {'cursor':updates_query.cursor()}]))
class CommentHandler(webapp.RequestHandler):
def post(self, update_id):
View
@@ -39,7 +39,6 @@
function loadMoreUpdates(cursor){
$.get('/updates/' + cursor, function(data) {
var foo = eval("(" + data + ")" );
- console.log(foo);
for(var i=0; i<foo[0].messages.length;i++){
$("#content")[0].innerHTML += renderUpdate(foo[0].messages[i])
}
@@ -61,7 +60,15 @@
u += "<a href='javascript:void()' onclick='toggleComment("+u.id+")'>Cancel</a>";
u += "</form>";
u += "</p>";
+ for(var i=0; i<update.comments.length; i++){u += renderComment(update.comments[i]);}
return u;
}
+
+ function renderComment(comment){
+ c = '<p style="margin-left: 20px;">';
+ c += comment.body + " | " + comment.ago + " | " + comment.user_fullname;
+ c += '</p>';
+ return c;
+ }
</script>
{% endblock %}

0 comments on commit cee4455

Please sign in to comment.