Backbone router with multiple parameters

matteok picture matteok · Nov 14, 2013 · Viewed 14.4k times · Source

I need to get this to work:

routes: {
  ':product' : 'showProduct',
  ':product/:detail': 'showProductDetail'

showProductDetail never gets called while the ':product' route is set even if it is set afterwards. I tried the following

routes: {
  ':product(/:detail)': showProductOrDetail
}

But this will not get called when only the second parameter changes. It is important that I have the product itself or the product and detail in the url.

Does anyone know how to fix this?

Answer

Szymon Drosdzol picture Szymon Drosdzol · Nov 14, 2013

There's a little hacky solution to your problem. I have a feeling there is a nicer way to do this but that should work:

routes: {
    "product/:id": "showProduct",
    "product/:id/details/:did": "showDetails"
},

showProduct: function(id) {
    this.showDetails(id);
},

showDetails: function(id, did) {
    // Check did for undefined

}