流星:订阅集合子集和集合总数

Meteor : subscribe to collection subset & collection total count

这是我的问题:

我正在 iron-router 中为 "infinite pagination" 订阅集合的子集(如 Discover Meteor 示例):

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
});

我发布的很好,没问题。但是,在同一页上,我还需要整个集合(不仅是子集)的总数。我这样发布:

Meteor.publish('applications', function(options){
   return Applications.find({}, options);
});

Meteor.publish('applicationsCount', function(){
    return Applications.find().count();
});

但是我想有些事情我没听懂。我需要在我的模板中使用总数,但我看不到如何在不创建新集合(我不想这样做)的情况下订阅 "just a number"。

我看过 Meteor Doc 上的 'counts-for-room' 示例,但它似乎与我需要的相去甚远(我没有空间放消息,我只需要计算我的应用程序而无需将它们全部放在客户端上)。

非常感谢,我希望我足够干净。 祝你有美好的一天。

如果要合集的计数

试试 publish-counts package

$ meteor add tmeasday:publish-counts

这就是您的代码应该看起来的样子。

 //server.js
Meteor.publish('applicationsCount', function() {
  Counts.publish(this, 'applicationsCount', Applications.find());
});

在 lib 文件夹中。

if(Meteor.isClient){
   Meteor.subscribe('applicationsCount')
   Counts.get('applicationsCount');
 }

现在看Counts.get就像一个帮手,所以你可以像这样在模板上使用它。

<span> There is a Total of {{getPublishedCount 'applicationsCount'}} Applications</span>

感谢 Ethann 我让它成功了。

首先我安装了publish-counts package

$ meteor add tmeasday:publish-counts

正如 Ethann 所说,我在 server\publications.js

上发布了计数
Meteor.publish('applicationsCount', function() {
  Counts.publish(this, 'applicationsCount', Applications.find());
});

然后我更新了我的 iron-router 控制器:

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
       };
   }
});

最终在我的模板中调用计数:

<span> There is a Total of {{getPublishedCount 'applicationsCount'}} Applications</span>

非常感谢。希望对身边的人有所帮助。