| « Would this work? | Best Complete Sentence Ever » |
Wednesday, October 7th, 2009
Jquery Comments
Categories: Life
I just can't say this enough. jQuery is AWESOME. I had a comment system in a django blog, which has supervisors and above moderate operator comments. With 37 lines of javascript (including comments and organizational line breaks) I was able to moderate the comments inline as shown here:
Inline Comment Moderation
Here is the jquery:
$(document).ready(function(){
change_links('');
});
//found this in some forum. it works for what i need, but is it good?
$.fn.unwrap = function(expr) {
return this.each(function(){
$(this).parents(expr).eq(0).after(this).remove();
});
};
function moderate_click(link){
var comment = link.closest('.comment');
comment.slideUp();
var id = comment.attr('id');
//get publish or remove
var actionurl = link.attr('href');
$.get(actionurl,function(data){
comment.wrap("<div id='one_time_use'></div>");
$('#one_time_use').html(data);
$('#'+id).unwrap("div");
change_links('#'+id);
$('#'+id).slideDown();
});
}
function change_links(param){
//the param gets the comments to change links
//ie either "" or the commentid
$(param + ' .moderate_links a').click(function(){
moderate_click($(this));
return false;
});
}
and here is the important part of the django view:
#this part is in both publish and remove views
if request.is_ajax():
return ajax_comment(request,comment,redirect)
#this is the ajax_comment function
def ajax_comment(request,comment,redirect):
if redirect.find('/bulletins/comments/') != -1:
blogslug = 'comments'
else:
blogslug = ''
return render_to_response('bulletins/comment.html',{
'comment':comment,
'user':request.user,
'blogslug':blogslug,
'is_ajax':True,
})
a call to those more experienced: is this good? is there a way to do this in only 10 lines of code? since i'm mostly teaching myself, i can only go on the basis of "It works!"
11:09am by Brandon //comment 1012 views