briangonzalez / codrops-medium-style-page-transitions

The repository for this article: http://tympanus.net/codrops/2013/10/30/medium-style-page-transition/
258 stars 40 forks source link

Nonsequential post IDs #1

Closed msurguy closed 7 years ago

msurguy commented 11 years ago

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:

this.getPost(index, function(d){
    self.nextPostID = d.nextID;
    self.contentizeElement(self['$' + type], d);
    callback && callback();
  });

And then changing the nextPostIndex function to this :

ArticleAnimator.nextPostIndex = function(index){
  return (index === this.postCount+this.firstPostIndex-1) ? this.firstPostIndex : this.nextPostID 
}

It works OK but it feels like there is a better way to do that... Could you point me to the right direction?

briangonzalez commented 11 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?

msurguy commented 11 years ago

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;
briangonzalez commented 11 years ago

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.

fvignals commented 10 years ago

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 ...

  1. If you use laravel, the paginator could help you, php cant create a link in php, html and even js file