I have a list of previewed posts on my page. When they're clicked on, they should load the full text of the post, load the comments, and slide down to reveal both.
I have one function, partialSlide, that simulates slideDown, but starting at a specified height - it's adapted from this answer. The other two functions, loadFullPost and loadComments are mine.
All the events are firing, but partialSlide is firing before loadComments is finished, so it's not taking them into account when calculating the height, and hence they're being cut off.
Why is that happening when I have them set up in a when > then format?
$.when(loadComments(divID,postID)).done(partialSlide(divID));
Am I misunderstanding how promises work in jQuery? Elsewhere in my code they've worked as expected (the then
only runs once the when
is done).
Full functions below:
function loadFullPost(permalink,divID,postID) {
$.when($.when($.ajax({url: permalink+"?ajax=true", success:
function(result){$("#"+divID+"").html(result);}})).then
(function( data, textStatus, jqXHR ) {
$.when(loadComments(divID,postID)).done(partialSlide(divID));
}));
}
function loadComments(divID,postID) {
$.ajax({url: "ajax-comments/?post_id=" + postID, success:
function(result){
$("#" + divID + " .comment.list").html(result);
}});
}
function partialSlide(divID) {
var contentHeight = $("#"+divID+"").addClass('full').height();
$("#"+divID+"").removeClass('full').animate({
height: (contentHeight == $("#"+divID+"").height() ? 100 : contentHeight)
}, 500);
}