Here is code from my app.
It is a loadMore pagination model for products.
I don’t use ReactiveVar. Just use normal variable and increment it when loadMore executed.
And when loadMore executed, I subscribe to new publication with new page and limit parameter to get the new products. So we get new products in client Collection.
See my code.
createContainer
const initPage = 1;
let nextPage = initPage;
let productsHandles = [];
let currentRoutePath = null;
export default createContainer((params) => {
const pageHandle = Meteor.subscribe('front.pages.products');
if (currentRoutePath !== FlowRouter.current().path) {
currentRoutePath = FlowRouter.current().path;
clearProducts();
}
const limit = 20;
//First subscription
const productsHandle = Meteor.subscribe('front.products.all', initPage, limit, tag);
productsHandles.push(productsHandle);
const countsHandle = Meteor.subscribe('front.products.count', tag);
const tagsHandle = Meteor.subscribe('front.tags.all');
const allProductsCount = Counts.get('front.products.count');
const data = {
loading: !productsHandle.ready() || !countsHandle.ready(),
page: Pages.findOne({name: 'products'}),
products: ProductsCollection.find({}, {sort: {code: 1, name: 1}}).fetch(),
allProductsCount,
isLoadMore: ProductsCollection.find().count() === allProductsCount,
//When loadMore add another subscription
loadMore(){
///
//Add another subscription
///
const productsHandle = Meteor.subscribe('front.products.all', ++nextPage, limit);
productsHandles.push(productsHandle);
}
};
return data;
function clearProducts() {
productsHandles.forEach((item) => item.stop());
productsHandles = [];
nextPage = initPage;
}
}, Products);
Subscription
Meteor.publish('front.products.all', function (page, limit, tag) {
check(page, Match.OneOf(Number, String));
check(limit, Match.OneOf(Number, String));
check(tag, Match.Maybe(String));
page = +page;
limit = +limit;
const skip = (page - 1) * limit;
const sort = {name: 1};
let filter = {};
if (tag) {
filter.tags = new RegExp(`^${tag}$`, 'i');
}
return Products.find(filter, {sort, skip, limit});
});
Meteor.publish('front.products.count', function (tag) {
check(tag, Match.Maybe(String));
let filter = {};
if (tag) {
filter.tags = new RegExp(`^${tag}$`, 'i');
}
Counts.publish(this, 'front.products.count', Products.find(filter));
});