Closed msurguy closed 7 years ago
Thanks for the kind words.
You bring up an interesting point. The question is, if not sequential, how do you determine which is the next post? What will your algorithm be?
The source of my app (using Laravel PHP framework as the quickest API for this type of blog) is posted here: https://github.com/msurguy/codrops-medium-style-page-transitions and the live app is at http://laramedium.gopagoda.com/
The algorithm I am using right now is the following (using Laravel's query scopes to filter out only posts that have 'published' set to true in DB):
$nextID = Post::published()->where('id', '>', $currentPost->id)->min('id');
This simply returns next ID from the DB by selecting the MIN from the IDs that are greater than the current one. It's a clever trick to get ID of the next ID in DB relative to the current one.
Another way to return the next post ID would be just by the published_at timestamp, whichever post is published after the current post... Something like:
$nextID = Post::published()->orderBy('published_at','asc')->where('published_at','>',$this->published_at)->first()->id;
Ah, I see.
There are lots of solutions here. One solution would be to override the nextPostIndex
method to make a quick Ajax call to your server and ask it what the next post id is, e.g. api/post/next_id
or something along those lines.
I would favor, however, override the getPost
method to return the post you needed. A little more complicated, but doable.
Be careful : ajax request on db is not easy to secure and peform well Personnaly, I DB request > output (json,xhtml,html) > Ajax read on the output. You create a static file (the output), that work like a cache, and this cache could be use with just html + ajax ... your site could work even 500 server error or db problem ...
This is a great write up, thank so much for posting it!
I was wondering how would I go about having nonsequential IDs for the next post (for example from DB), apart from having that data in the JSON of the post, what would I need to change in the JS? I kinda got it to work by adding nextPostID to createPost function:
And then changing the nextPostIndex function to this :
It works OK but it feels like there is a better way to do that... Could you point me to the right direction?