【问题标题】:Pagination based on date in meteor, similar to Product Hunt基于流星中日期的分页,类似于 Product Hunt
【发布时间】:2015-09-17 15:12:56
【问题描述】:

我正在关注 Discover Meteor.js 书并创建链接共享应用程序,但我希望基于周而不是按新近度进行分页。

目前代码的结构是根据 URL 显示一定数量的帖子:http://localhost:3000/

但我想显示最近一周提交的每个帖子,然后是前一周,等等。

这里是发布的帖子 mongo 集合:

Meteor.publish('posts', function(options){
  check(options, {
    sort: Object,
    limit: Number,
  });
  return Posts.find({}, options);
});

这是路由器将数据传递给客户端的方式

PostsListController = RouteController.extend({
  template: 'postsList',
  increment: 5,
  postsLimit: function() {
    return parseInt(this.params.postsLimit) || this.increment;
  },
  findOptions: function() {
    return {sort: {submitted: -1}, limit: this.postsLimit()};
  },
  subscriptions: function() {
    this.postsSub = Meteor.subscribe('posts', this.findOptions());
  },
  posts: function() {
    return Posts.find({}, this.findOptions());
  },
  data: function() {
    var hasMore = this.posts().count() === this.postsLimit();
    var nextPath = this.route.path({postsLimit: this.postsLimit() + this.increment});
    return {
      posts: this.posts(),
      ready: this.postsSub.ready,
      nextPath: hasMore ? nextPath : null
    };
  }
});

与 Product Hunt 类似,我如何按周对帖子进行分组、将该信息编码到 URL 并在页面底部创建一个链接以查看前一周的帖子?

谢谢!

【问题讨论】:

    标签: meteor pagination


    【解决方案1】:

    基本流程:

    1. 将您的路线参数:numberOfDisplayedPosts 替换为:startdate,以便您的路线知道使用哪个日期作为起点。为了获得更大的灵活性,您可以使用两个路由参数 startdate 和 enddate,然后您可以查看周数、天数、月数等等。
    2. 删除所有对limit 的引用,因为您将只使用日期范围。
    3. 根据您的路线参数计算开始和结束日期时间。请注意,mongodb 以 UTC 存储日期时间。
    4. 使用查询中的这些日期时间在日期范围内进行选择。
    5. 计算下一页/上一页路由所需的参数。
    6. 您还必须基本上运行下一页/上一页查询以查看那里是否有任何数据,因为无法判断您是否已经到达终点。或者,您可以使用最小/最大日期时间来计算。
    7. 在客户端而不是服务器上进行排序,除非您尝试获取整个集合的最小值或最大值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2011-01-22
      • 2020-05-15
      • 1970-01-01
      • 2015-10-07
      • 2011-03-31
      相关资源
      最近更新 更多