Here is my issue :
I'm subscribing to a subset of a Collection for "infinite pagination" in iron-router (like the Discover Meteor example) :
ApplicationsListController = RouteController.extend({
template: 'applicationsList',
increment: 10,
limit: function(){
return parseInt(this.params.applicationsLimit) || this.increment;
},
findOptions: function(){
return {sort: {name: 1}, limit: this.limit()};
},
subscriptions: function(){
this.applicationsSub = Meteor.subscribe('applications', this.findOptions()) ;
},
applications: function(){
return Applications.find({}, this.findOptions());
},
data: function(){
var hasMore = this.applications().fetch().length === this.limit();
var nextPath = this.route.path({applicationsLimit: this.limit() + this.increment});
return {
applications: this.applications(),
ready: this.applicationsSub.ready,
nextPath: hasMore ? nextPath : null
};
}
});
//(...)
this.route('applicationsList', {
path: '/applications/:applicationsLimit?',
controller: ApplicationsListController
});
I'm publishing it well, no problem there. But, on the same page, I also need the total count of the entire collection (not only the subset). I publish it like that :
Meteor.publish('applications', function(options){
return Applications.find({}, options);
});
Meteor.publish('applicationsCount', function(){
return Applications.find().count();
});
But there is something I guess I did not understand. I need to use the total count in my template, but I just can't see how to subscribe to "just a number", without creating a new collection (which I don't want to do).
I've seen the 'counts-for-room' example on Meteor Doc, but it seems that it is far from what I need (I don't have room with message in it, I just need to count my applications without getting them all on client).
Thanks a lot, I hope I was clean enough. Have a great day.
Thanks to Ethann I made it work.
First I installed the publish-counts package
$ meteor add tmeasday:publish-counts
As Ethann said, I published the count on my server\publications.js
Meteor.publish('applicationsCount', function() {
Counts.publish(this, 'applicationsCount', Applications.find());
});
And I updated my iron-router
Controller like that :
ApplicationsListController = RouteController.extend({
template: 'applicationsList',
increment: 10,
(...)
subscriptions: function(){
this.applicationsSub = Meteor.subscribe('applications', this.findOptions()) ;
this.applicationsCount = Meteor.subscribe('applicationsCount');
},
(...)
data: function(){
var hasMore = this.applications().fetch().length === this.limit();
var nextPath = this.route.path({applicationsLimit: this.limit() + this.increment});
Counts.get('applicationsCount');
return {
applications: this.applications(),
ready: this.applicationsSub.ready,
nextPath: hasMore ? nextPath : null
};
}
});
To finally call the count in my template:
<span> There is a Total of {{getPublishedCount 'applicationsCount'}} Applications</span>
Thanks a lot. Hope it will help some people around here.