MeteorJS + Iron-Router: How can I call the next it

2019-03-22 09:23发布

I did a lot of research before posting this question and couldn't find the answer. I'm sure it's a simple task, but it's going over my head for some reason.

I have a "posts" type page that lists all items in my collection, and a "single post" page to display the contents of the selected post. I have two pagination arrows for going to the previous item and next item. How can I grab the URL for the previous/next posts and display it in my template?

Any help would be appreciated, thanks!

2条回答
劳资没心,怎么记你
2楼-- · 2019-03-22 09:38

You can do this without using iron router pathFor

by using this package https://github.com/alethes/meteor-pages it give a varies options to use a paginations

Also you may be interest to check this URL's Best pattern for pagination for Meteor http://www.youtube.com/watch?v=WSwe_weshQw

查看更多
成全新的幸福
3楼-- · 2019-03-22 09:42

I created sample application which exactly do what you need. Please check it : https://github.com/parhelium/meteor-so-post-next-prev/tree/master

Briefly explanation of important parts of this code ( see repo to find more details ):

Server Side

First you need to have some model:

 {title:"1 post", createdAt:moment("01-10-1995", "DD-MM-YYYY").toDate()}

and publish functions:

Meteor.publish("postDetailsNext",function(postId){ });
Meteor.publish("postDetailsPrev",function(postId){ });
Meteor.publish("postDetails",function(postId){});

The 'tricky' part is how to write queries for taking next and prev posts.

Meteor.publish("postDetailsNext",function(postId){

      // load post object which should be displayed in 'postDetails' route
      var post = Posts.findOne({_id:postId});

      // find one post from sorted cursor which is older than main post 
      return Posts.find({createdAt:{$gt:post.createdAt}},{sort:{createdAt:1}, limit:1})
});

and similar :

Meteor.publish("postDetailsPrev",function(postId){  
      var post = Posts.findOne({_id:postId});
      return  Posts.find({createdAt:{$lt:post.createdAt}},{sort:{createdAt:-1}, limit:1})
  });

Note that queries from above publish functions differs in sort field.

Client Side

this.route('postDetails',{
      where:'client',
      path:'/post/:_postId',
      template:'postDetails',
      waitOn:function(){
        return [
            Meteor.subscribe('postDetails', this.params._postId),
            Meteor.subscribe('postDetailsPrev', this.params._postId),
            Meteor.subscribe('postDetailsNext', this.params._postId)
        ]
      },
      data:function(){
        var post = Posts.findOne({_id:this.params._postId});
        if(post == null) return {};
        else
            return {
                post : Posts.findOne({_id:post._id}, {limit:1}),
                prev : Posts.findOne({createdAt:{$lt:post.createdAt}},{sort:{createdAt:-1}}),
                next : Posts.findOne({createdAt:{$gt:post.createdAt}},{sort:{createdAt:1}})
            }
        }
      }
    })
查看更多
登录 后发表回答