2014-01-14

This tutorial is one more addition to my “Building a shop with Laravel” tutorial series.

In the previous tutorial, I showed you how to implement very nice “Smart search” feature using Selectize.js plugin to make user experience a bit nicer: http://maxoffsky.com/code-blog/laravel-shop-tutorial-3-implementing-smart-search/

When I develop web applications, I care a lot about the user experience and how easy it is for the user to get to the desired page.

In this post I will explain the concepts behind making in-place pagination for your PHP apps that allows the user to view many pages of content without doing a page refresh, aka “In-place pagination”.

The real, working demo of this type of search is also available at:
http://laravel-shop-pagination.gopagoda.com/

And the source for the demo is at https://github.com/msurguy/laravel-backbone-pagination

See the demo GIF below:



Let’s start with some basics:

There are many ways to achieve this kind of pagination user experience in your web applications using Javascript and AJAX calls. The underlying concept is that there is a Javascript frontend that requests a set of data, processes the result and updates the user interface with the new data. What kind of information does the frontend need to update the user interface for the pagination accordingly?

Data necessary for the pagination:

Number of items in total

Number of pages (this value could be calculated from the number of items/how many items to show)

Index of the current page

Type of sorting (in case the user wants to sort the items differently)

Items themselves (to populate the user interface such as a grid or a list, etc)

Knowing this information, we can proceed with looking at the implementation of the concept using Backbone.Paginator.

What is Backbone.Paginator?

Backbone Paginator is a small plugin for Backbone.js JS MVC framework. If you are not familiar with Backbone or its bigger competitor – AngularJS, I highly recommend you look into the available JS MVC framework options. Backbone.js is just one of the many frameworks that allow building Single Page web Applications (SPA’s). I use Backbone in many of my projects to integrate parts of single page application snappiness(responsiveness) into my applications. Backbone Paginator allows easy integration with Backbone to provide pagination methods for the collections of data.

To integrate Backbone Paginator you need to download the minified production version from the project homepage:

http://backbone-paginator.github.io/backbone.paginator/ 

And of course you need Backbone itself and the Underscore library to be included in your project as well. For better user experience, I included Spin.js plugin to show the user that the page is being updated.

Here’s a code snippet that I used when all of the JS dependencies were downloaded:

Building the API for the paginator:

The API for the paginator is very simple. It needs to return the list of items as JSON data, along with the total count of items, depending on the index of the page that the user is requesting. Backbone.Paginator allows you to provide a parameter that will be passed to the API for sorting purposes. Other two parameters that will be passed to the API from the paginator are the index of the current page and the number of records to show per page.

Based on these three parameters and the desired output information, the API looks like this (using Laravel’s scopes to return an appropriate set of data), commented:

That’s it for the API! Make sure you place this file as ProductsController.php in the app/controllers/Api folder and also add this line to the routes.php file to route the requests to it:

Building out the Javascript templates for the Backbone.Paginator:

If you are familiar with Backbone, it uses Underscore.js templates to make creation of view templates very easy. Backbone.Paginator plugin uses the same kind of templates to present the pagination interface to the user. These templates will all go into the view file containing the logic for the Javascript MVC part of the application.  There are two templates that the plugin is using:

Sorting interface template – to show the user sorting options (for example a dropdown menu)

Pagination layout template – to show the user number of pages, previous and next links and more

A small diagram showing these two templates rendered:



Another template in addition to these two is the item template itself (to show the item from the data). Here is how it looks like:



The two templates required for the Backbone.Paginator using Bootstrap 3 HTML/CSS framework are presented below:

These templates will be used in the Backbone views and connected to the events dispatched by the views when the user interacts with the templates. Let’s build out the result item template (the template used to display a single item of the data). In this case I was building a shop so the item shows a product from the shop.

Building the result item template

This code could be easily adapted to your own item display. It uses a mix of Blade and underscore templating. The Blade is used in only one place – to display the URL of the shop item which ideally should’ve been provided from the API call. The underscore templating replaces the <% %> and <%= %> with the output of javascript variables that will be fed into the template from the JSON data returned from the API.

Building the Backbone Application

Now that we have the view templates ready to go, let’s connect everything together by building a small Backbone application that will use the API to sort and paginate data. I am not a Javascript ninja so for most of this code I just followed the examples provided in Backbone.Paginator repository with some minor modifications. Let’s dive into the Javascript code right away.

For the initial setup, let’s create a global object to store our Backbone application:

Now, let’s define an empty Backbone model for the item (product in my case):

And a paginated collection according to the Backbone.Paginator documentation:

Next, let’s create a Backbone view that will render an item template when the view is initiated and passed a model into the view. This view will be the one responsible for create a DIV element and assigning classes to it so that it works nicely in Bootstrap 3 grid:

Well, since our application has an option to sort the items by some criteria and Backbone.Paginator plugin has ability to work with the sorting methods, let’s also build a template for the sorting user interface. This view will listen to a click event on the sorting options and when the event is dispatched, the Backbone collection containing items will be updated by passing the sorting option to our API and retrieving the results. Here’s the code for the Backbone view for the sorting interface:

It’s coming along and we are almost finished! Just two more Backbone views to make and we are done with this nice tutorial!

Perhaps the most important Backbone view in this application is the one responsible for updating the pagination user interface and showing prev/next links. This view is tightly integrated with the methods that Backbone.Paginator provides to paginate a collection such as “requestPreviousPage()”,  ”requestNextPage()” and “goTo(page)”. Let’s take a look at this view, called “PaginatedView” :

And now, the last Backbone view responsible for rendering the items when the paginated collection is updated, the AppView:

This is it for the backbone application model/views/collection definitions! To initialize this app when the page is loaded, let’s put this code into the jQuery’s document ready function:

This code will initialize the collection for the items, the app view, pagination view, sorting view and you should see the items appearing upon document load!

Of course if you are brand new to Backbone, this tutorial might be a little overwhelming and a lot of concepts will be new to you. In that case I highly recommend you check out Backbone.js documentation here.

If you have questions about anything here feel free to ask me in the comments!

As always, the source code for this tutorial is on Github:

https://github.com/msurguy/laravel-backbone-pagination

Also, if you know anybody who’s getting into Laravel please tell them about my book, “Laravel in Action” that will be published by Manning Publications, the first 4 chapters are already done and available:

Show more