Search code examples
backbone.jsbackbone-routing

2 paginations on one page - Backbone Routes


I need to handle 2 separate paginations on one page.

At the moment I have only one pagination, the anchors contain just plain hashtags which are parsed by the Route Object like so

HTML

<a href="#1"></a>

Backbone Router

routes: {
        ":number": "paginate"
    }

How would I handle two paginations at once?


Solution

  • What I ended up doing was I wrote a function that parses hash query strings and triggers an event via an aggregator which then gets picked up by whoever needs to react to pagination. In my case it's a collection that clears its' items and adds the next page...

    Here's the router in case it helps:

    define(
        function(){
    
        var r_Paginate = Backbone.Router.extend({
    
        /* Initialise Object */
            initialize: function(options){
                Backbone.history.start();
            },
        /* Events */
            routes: {
                ":any" : "query",
            },
    
        /* Object Methods */
            query: function(a){
                if(a.indexOf('&') > -1){
                    var queries = a.split('&');
                    for (x in queries){
                        var query = queries[x].split('=');
                        Z.events.trigger('paginate:' + query[0], {'page' : query[1]});
                    }
                }
            }
    
        });
        return r_Paginate
    });