diff --git a/public/src/app.js b/public/src/app.js index eb5e2b51a3..878e569800 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -333,13 +333,15 @@ var socket, app.infiniteLoaderActive = false; if(data.posts.length) { app.createNewPosts(data); - if(callback) - callback(); } + + if(callback) + callback(data.posts); }); } app.scrollToPost = function(pid) { + if(!pid) return; @@ -347,17 +349,31 @@ var socket, scrollTo = $('#post_anchor_' + pid), tid = $('#post-container').attr('data-tid'); - while(!scrollTo.length && tid) { - app.loadMorePosts(tid, function() { - scrollTo = $('#post_anchor_' + pid); - }); - } - - if(tid) { + function animateScroll() { $('body,html').animate({ scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop() - $('#header-menu').height() }); } + + if(!scrollTo.length && tid) { + + var intervalID = setInterval(function() { + app.loadMorePosts(tid, function(posts) { + scrollTo = $('#post_anchor_' + pid); + + if(tid && scrollTo.length) { + animateScroll(); + } + + if(!posts.length || scrollTo.length) + clearInterval(intervalID); + }); + }, 100); + + } else if(tid) { + animateScroll(); + } + } jQuery('document').ready(function() { diff --git a/src/websockets.js b/src/websockets.js index 542c20135f..f4cb3626a6 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -628,10 +628,11 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); socket.on('api:topic.loadMore', function(data, callback) { - var start = data.after, + var start = data.after, end = start + 9; topics.getTopicPosts(data.tid, start, end, uid, function(posts) { + console.log('sending back tid: '+data.tid, posts); callback({posts:posts}); }); });