我的分页有什么问题(流星)

时间:2015-04-08 20:26:45

标签: meteor iron-router

我的router.js(我使用铁路由器):

...
PostsListController = RouteController.extend({
    template: 'postsList',
    increment: 4,
    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
        };
    }
});
NewsController = PostsListController.extend({
    template: 'newsTemplate',
    increment: 2,
    limit: 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({postType:'Новости'}, 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
        };
    }
});
Router.route('/news/:postsLimit?', {
    name: 'newsTemplate',
    controller: NewsController
});

Router.route('/:postsLimit?', {
    name: 'postsList'
});
...

我的模板(与索引模板相同,仅在名称上有所不同):

<template name="newsTemplate">
    <div class="container main">
    <div class="row">
        {{#each posts}}
            {{> newsItem}}
        {{/each}}

    </div>
    <div class="col-md-12">
        {{#if nextPath}}
            <div class="row">
                <a class="load-more" href="{{nextPath}}" style="color: black;font-size: 1.3em;"><button type="button" class="btn btn-default btn-block">Load more</button></a>
            </div>
        {{else}}
            {{#unless ready}}
                {{> spinner}}
            {{/unless}}
        {{/if}}
    </div>
        </div>
</template>

在索引页面上(使用PostsListController)它运行良好,但在url http://localhost:3000/news(using NewsController上:

  • 我收到了正确的帖子,但是按钮&#39;加载更多&#39;不工作(点击时什么都不做)。

在网址http://localhost:3000/news/2(或新闻/ 1,3等):

  • 它工作正常并向我显示相应数量的帖子,按钮&#39;加载更多&#39;工作也很

问题是什么?为什么按钮不能用于http://localhost:3000/news

1 个答案:

答案 0 :(得分:1)

在您的NewsController中,您已将postsLimit函数的名称更改为限制:

limit: function() {
    return parseInt(this.params.postsLimit) || this.increment;
},

但是在您的数据功能中,您仍然指的是this.postsLimit():

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

将限制函数更改为newsLimit以使其更清晰,然后更改数据函数以使用this.newsLimit()。