How to get the id of the next document in the subscription


#1

I have a simple one-page parallax site. Each section template is populated by a document, and each section has a button that when clicked auto scrolls to the next section. I’m having a problem targeting the next section. I have the doc _id as the id attribute for the sections but how do I get the next one dynamically?

For example if this is my button markup within my template, this is what I’m trying to get:

<a href="{{ _id+1 }}" class="btn-medium">
    <i class="fa fa-arrow-down fa-2x"></i>
</a>

I know _id+1 doesn’t do anything, but that’s the goal, get the id of the next document within the subscription.


#2

Server

Meteor.publish('next', function(limit) {
  check(limit, Number);
  return collection.find({},{
     sort: {name: -1},
     limit: limit
  });
});

Client

Session.setDefault('nextPage', 10);
Tracker.autorun(function() {
   Meteor.subscribe('next', Number(Session.get('nextPage')))
})

If the button was clicked just increase the session and get more results.


#3

There is no direct way to do that. All the data for all the subscriptions of each collections gets merged and goes into a single minimongo collection.

So, you need to select your dataset with a query.

Then you can use each in template to get this items.

Otherwise you can use the mongodb’s skip and limit options. Check the meteor docs for skip and limit options. With that you can get the next item.


#4

Add new field in collection with “categories” and “documentindex” and you could retrieve the next document using find()


#5

@proyb2 Can’t imagine your solution. Please, show an exampe, because I had relevant issue and solved with skip and limit - but it is not really optimal way.


#6

http://www.webtempest.com/meteor-js-autoform-tutorial

I follow this tutorial on adding new schema using autoform which could be applicable to adding new fields for “categories” as IT…Business…Financial and “documentindex” as 1…2…3…4…5. Safer and good housekeeping.

_id is a random alphanumeric so the threadstarter of this question can’t retrieve the next documents in sequence order.


#7

@proyb2 Got it! But using {sort: [<my custom sort>]} for query brokes that kind of logic. Moreover this way you have to publish documentindex for the client.


#8

Thanks everyone for the response. In the end I solved it by streamlining my DOM and using jQuery to find the next target to scroll to. I had another use for this but in that instance using fullpage.js solved my problems.


#9

Is it similar to Shopify using DOM scrapping method with Jquery?


#10

I’ve never used Shopify. Once I cleaned up my code a bit it became easier to traverse the DOM with jQuery. still pretty awkward though.


#11

+1 for client side. Using nextNode with current active DOM element.
But the question is How to get next document from subscription/query.