imJohnBon's avatar

Best way to implement a load more button?

Simple and common example: I have a job posting board where I use Laravel's default pagination to render 10 at a time. At the bottom of the page I want to make an ajax call to get the next 10 and append them to the bottom of the current list.

What's my best way of going about doing this? Surprisingly, I haven't found any solid examples online.

In some things I have seen, people request doing something like this (in regards to getting the data):


function getJobs()
{
    if (Request::ajax())
    {
        return Jobs:all();
    }

    return View::make(...);
}

Basically, intercepting calls made over Ajax and returning JSON instead of the view. But what do I then do with the returned JSON? Do I need to parse it in Javascript and then append it to the list? That's pretty less than ideal as I'm duplicating markup inside my javascript.

I know an old method would be to do something like this in jQuery:


    jQuery('#ajax-destination').load('http://www.example.com/jobs?page=3 #ajax-content');

But isn't this also less than idea since you're basically doing a full page call every time? Is there a happy middle ground?

0 likes
1 reply

Please or to participate in this conversation.