【问题标题】:Setting up simple pagination for a meteor app using Iron router and Pagination.collection()?使用 Iron 路由器和 Pagination.collection() 为流星应用程序设置简单的分页?
【发布时间】:2015-05-03 17:58:00
【问题描述】:

您好,我有一个简单的应用程序列表帖子,我正在努力使用 Iron Router 和 Meteor Pagination 添加一个简单的分页:https://github.com/egtann/meteor-pagination

我的 index.js 中有以下内容:

if (Meteor.isClient) {
    Template.index.helpers({

        posts: function () {
            return Posts.find({}, {sort: {voteResult: -1}});
        },
}

因此要在我的 index.html 中显示我使用 {{#each posts}} 的帖子,如下所示:

      <template name="index">
            <ul>
            {{#each posts}}
                <li>
                   {{text}}
                </li>
            {{/each}}       
            </ul>
<!-- pagination BOOTSTRAP CSS -->
    <!--    <div class="text-center">
            <ul class="pagination_bis">
              <li>
                <a href="#" aria-label="Previous">
                  <span aria-hidden="true">&laquo;</span>
                </a>
              </li>
              <li class="active_bis"><a href="#">1</a></li>
              <li><a href="#">2</a></li>
              <li><a href="#">3</a></li>
              <li><a href="#">4</a></li>
              <li><a href="#">5</a></li>
              <li>
                <a href="#" aria-label="Next">
                  <span aria-hidden="true">&raquo;</span>
                </a>
              </li>
            </ul>
        </div> -->
        </template>

这是我的 route.js

Router.configure({
    loadingTemplate: 'loading',
    notFoundTemplate: 'notFound',
    layoutTemplate: 'layout'
});

Router.route('/', function() {
    this.render('index');
});

我的问题:

  • 如何在 routes.js (Iron Router) 中定义我的路由以进行简单的分页?
  • 如何使用我的 index.js 中的路由和使用 Meteor 分页(上面提供的链接)的助手,以便它仅基于每页返回有限数量的帖子?
  • 如何将此逻辑连接到视图 (index.html)?

我尝试按照上面提供的链接示例进行操作,但我很困惑,他们使用的 Meteor 版本已被弃用。不用说,我是 Meteor 的新手,每一个输入都非常感谢。供您参考,应用程序事件工作正常,我设法保存和删除帖子,为测试分页创建了 30 多个帖子。谢谢。

【问题讨论】:

    标签: meteor pagination iron-router


    【解决方案1】:

    我不会使用不再维护的软件包(egtann:meteor-pagination 的最后一次提交是在 2 年前)。

    只需尝试alethes:pages (GitHub project)。似乎也有很好的记录。

    至于路由器。您可能必须使用 URL 参数(如果您想通过 URL 直接访问特定页面 - 例如第 5 页的 /posts/5/):

    Router.route('/posts/:page', function () {
      var pageId = this.params.page;
      ...
    });
    

    【讨论】:

    • 你能告诉我/告诉我如何在 html 中设置 :page 等于 postId 以便它呈现像 /posts/:page 这样的正确路线吗?
    • 不幸的是,这不是我的主域。我认为您可能需要一个控制器(在 Iron Router 的上下文中搜索它)。有一个人这样做的例子:crater.io/top/40 - 你可以检查源代码并搜索/top/: 以找到路由声明以及他们如何解决它。
    • 感谢彼得的帮助
    【解决方案2】:

    对于初学者来说,这是一个了解动态路线以及如何在 Meteor 中将数据用于路线的绝佳资源:http://meteortips.com/tutorial/iron-router-part-2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-16
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2015-12-08
      • 1970-01-01
      相关资源
      最近更新 更多