Question

I'm moving my django implementation slowly to backbonejs rendering. Django is used as a fallback when backbonejs rendering cannot handle. This is taken care by backbonejs router using the following code.

Backbone.history.start({
    pushState: true,
    silent: true
});

$(document).on("click", "a[href^='/']", function(event) { 
  if (!event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) { 
    var url = $(event.currentTarget).attr("href").replace(/^\//, "");
    var matched = _.any(Backbone.history.handlers, function(handler) { 
      if (handler.route.test(url)) { 
        return true;
      } 
    });

    if (matched) { 
      event.preventDefault();
      application.router.navigate(url, { trigger: true });
    } 
  } 
});

Basically if backbone routers cannot handle it will fallback to django template rendering. But I'm now unable to catch pages which are paginated. I've the following routing defined.

module.exports = Backbone.Router.extend({

  routes: {
    'set/(?page=:page)': 'list_set'
  },

  list_set: function(page) {
    // Do listing of set
  }
});

I would prefer urls with set/?page=2 to get triggered by Backbone.js. But the above route fails to match. What is the routing pattern which will make page parameter optional and also make backbonejs handle routing?

Was it helpful?

Solution

The routing pattern used was correct. The problem was found to be in the way the anchor href was defined in the html page.

The anchor element was defined as follows:

<a class="page" href="?page=2" data-original-title="" title="">2</a>

The href value was ?page=2 which starts with ?. But the event listener was expecting to catch click events of anchor only where href starts with /. Hence it failed to preventDefault!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top