我在这里跟着教程第一最新数据,至信:http://www.meteorpedia.com/read/Infinite_Scrolling,其结果如预期通过本教程的作者,即无限滚动显示第一数据(旧)收集到最后(最新),从上到下。为了实现无限滚动的Meteorjs,显示
问题是:我想要实现无限滚动的地方在新闻源中,就像在Facebook中发现的那样,它显示来自集合的最新(最新)数据,当您向下滚动,旧数据将被添加。
我试着使用createdAt:-1
对数据进行排序倒退,但一个有趣的事情发生了:
在刷新时,新闻源将显示前3个旧数据(因为我把3为限),并然后当我向下滚动,另外3组数据(更新不最新)将在TOP旧数据的追加,并且这种模式继续,直到数据完全加载在屏幕上。我想实现的是类似Facebook的资讯供稿即新闻源显示最新/近期数据在顶部,并作为用户向下滚动,较旧的数据被调用,并添加到客户端。作为下面提供的代码:
statusBox.html(客户端)
<template name="statusBox">
{{#each newsfeedList}}
..HTML template goes here..
{{/each}}
{{#if moreResults}}
<div id="showMoreResults" class="text-center" style="margin-left: 25px;">
<span class="loading"><i class="fa fa-spinner fa-pulse" aria-hidden="true"></i></span>
</div>
{{/if}}
</template>
publish.js(服务器)
Meteor.publish('status', function(limit){
return Status.find({}, {limit:limit});
});
statusBox.js(客户端)
newsfeed_increment = 3;
Session.setDefault('newsfeedLimit', newsfeed_increment);
Deps.autorun(function(){
Meteor.subscribe('status', Session.get('newsfeedLimit'));
});
Template.statusBox.helpers({
//Merging few collections into one template helper: https://stackoverflow.com/questions/21296712/merging-collections-in-meteor
newsfeedList: function(){
return Status.find({},{sort:{createdAt:-1}});
},
...
//Reference for infinitescrolling: http://www.meteorpedia.com/read/Infinite_Scrolling
moreResults: function() {
// If, once the subscription is ready, we have less rows than we
// asked for, we've got all the rows in the collection.
return !(Status.find().count() < Session.get("newsfeedLimit"));
}
});
// whenever #showMoreResults becomes visible, retrieve more results
function showMoreVisible() {
var threshold, target = $("#showMoreResults");
if (!target.length) return;
threshold = $(window).scrollTop() + $(window).height() - target.height();
if (target.offset().top < threshold) {
if (!target.data("visible")) {
// console.log("target became visible (inside viewable area)");
target.data("visible", true);
Session.set("newsfeedLimit",
Session.get("newsfeedLimit") + newsfeed_increment);
}
} else {
if (target.data("visible")) {
// console.log("target became invisible (below viewable arae)");
target.data("visible", false);
}
}
}
// run the above func every time the user scrolls
$(window).scroll(showMoreVisible);
结果 之前滚动,3,4,5,6顺序。注意新增设箱4,5,6,对箱1,2,3的顶掉的图片)
漂亮!这解决了问题!谢谢@jamider! –