试试这个发布功能,
Meteor.publish('posts', function(limit) {
if (limit > Posts.find().count()) {
limit = 0;
}
return Posts.find({ },{limit:limit});
});
现在在 client.js
上
Template.Posts.created = function() {
Session.setDefault('limit', 10);
Tracker.autorun(function() {
Meteor.subscribe('getPosts', Session.get('limit'));
});
}
现在你可以使用这个助手了,
Template.Posts.helpers({
posts: function() {
return Posts.find({ }, { limit: Session.get('limit') });
}
});
并像任何普通助手一样在每个助手上使用它
<template name="Posts">
{{#each posts}}
{{namePost}} <!-- or whatever register on the posts mongo document -->
{{/each}}
<!-- button to load more posts -->
<button class="give-me-more">Click for more posts </button>
</template>
现在,如果您想将帖子数量增加 10 x 10,请使用此功能
incrementLimit = function(inc=10) {
newLimit = Session.get('limit') + inc;
Session.set('limit', newLimit);
}
并在这样的点击事件上调用它
Template.Posts.events({
'click .give-me-more': function(evt) {
incrementLimit();
}
});
现在,每次创建帖子模板时,您使用此帮助程序的每个模板只会获得 10 个帖子,并且每次单击按钮时加载 10 次
这是来自Gentlenode的相同代码
我刚刚添加了 HTML,希望对您有所帮助