From 4c1f6595a3eb25f821b73bf04be964351a29a306 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Tue, 29 Oct 2013 16:55:59 +0900 Subject: [PATCH] =?UTF-8?q?finally=20got=20it=20to=20work!=20(I=20hope?= =?UTF-8?q?=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/helpers/router.js | 4 +- client/views/posts/post_item.js | 70 ++++++++++++-------------------- client/views/posts/posts_list.js | 17 ++++---- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/client/helpers/router.js b/client/helpers/router.js index 8d20c267f..b11523d69 100644 --- a/client/helpers/router.js +++ b/client/helpers/router.js @@ -255,14 +255,14 @@ PostsListController = RouteController.extend({ Session.set('postsLimit', limit); // get posts and decorate them with rank property - // note: not actually used; find better way + // note: not actually used; // posts = posts.map(function (post, index) { // post.rank = index; // return post; // }); return { - posts: posts + postsList: posts } }, after: function() { diff --git a/client/views/posts/post_item.js b/client/views/posts/post_item.js index bfb6de139..6c02ca4bb 100644 --- a/client/views/posts/post_item.js +++ b/client/views/posts/post_item.js @@ -77,54 +77,38 @@ Template.post_item.helpers({ } }); -// var recalculatePosition = function ($object) { -// var pArray = $object.data('positionsArray'), -// top = $object.position().top; - -// if(typeof pArray !== 'undefined'){ +var recalculatePosition = function ($object, pArray) { + // delta is the difference between the last two positions in the array + var delta = pArray[pArray.length-2] - pArray[pArray.length-1]; -// // if current position is different from the last position in the array, add current position -// if(top != pArray[pArray.length-1]){ -// pArray.push(top); -// $object.data('positionsArray', pArray); -// } + // if new position is different from previous position + if(delta != 0){ + // send object back to previous position + $object.removeClass('animate').css("top", delta + "px"); + // then wait a little and animate it to new one + setTimeout(function() { + $object.addClass('animate').css("top", "0px") + }, 1); + } +} -// // delta is the difference between the last two positions in the array -// var delta = pArray[pArray.length-2] - pArray[pArray.length-1]; +Template.post_item.rendered = function(){ + var instance = this, + $instance = $(instance.firstNode.nextSibling), + top = $instance.position().top; -// // if new position is different from previous position -// if(delta != 0){ + // if this is the first render, initialize array, else push current position + if(typeof instance.pArray === 'undefined'){ + instance.pArray = [top] + }else{ + instance.pArray.push(top); + } -// // send object back to previous position -// $object.removeClass('animate').css("top", delta + "px").addClass('animate'); + // if this is *not* the first render, recalculate positions + if(instance.pArray.length>1) + recalculatePosition($instance, instance.pArray); -// // then wait a little and animate it to new one -// setTimeout(function() { -// $object.css("top", "0px") -// }, 1500); - -// } -// } -// } - -// Template.post_item.rendered = function(){ - -// var instance = this, -// $instance = $(instance.firstNode.nextSibling), -// top = $instance.position().top; - -// instance.renderCount = (typeof instance.renderCount === 'undefined') ? 1 : instance.renderCount+1; - -// if(instance.renderCount>1){ -// // when one post re-renders, force all of them to recalculate their position -// $('.post').each(function(index, item){ -// recalculatePosition($(item)); -// }); -// }else{ -// $instance.data('positionsArray', [top]); -// } - -// }; +}; Template.post_item.events = { 'click .upvote-link': function(e, instance){ diff --git a/client/views/posts/posts_list.js b/client/views/posts/posts_list.js index 2b1138660..1c23d11bc 100644 --- a/client/views/posts/posts_list.js +++ b/client/views/posts/posts_list.js @@ -1,13 +1,12 @@ Template.posts_list.helpers({ - // posts : function () { - // not used, forces multiple re-renders - // this.postsList.rewind(); - // var posts = this.postsList.map(function (post, index, cursor) { - // post.rank = index; - // return post; - // }); - // return posts; - // }, + posts : function () { + this.postsList.rewind(); + var posts = this.postsList.map(function (post, index, cursor) { + post.rank = index; + return post; + }); + return posts; + }, allPostsLoaded: function(){ return false; // TODO: find out when all posts have been loaded