Backbone Router with Multiple Settings

I need to make this work:

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

showProductDetail is never called while the ": product" route is installed, even if it is installed after that. I tried the following

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

But this will not be caused when only the second parameter changes. It is important that the product has the product itself or the product and details in the URL .

Does anyone know how to fix this?

+6
source share
2 answers

There's a little hacky solution to your problem. I have a feeling that there is a better way to do this, but this 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 } 
+16
source

Late answer (more than a year) .. but you can use RegEx in the base router to achieve this. My example assumes parameters start with a number.

i.e.: localhost: 8888 / # root / 1param / 2param

 var router = Backbone.Router.extend({ initialize: function () { // Use REGEX to get multiple parameters this.route(/root/, 'page0'); this.route(/root\/(\d+\S+)/, 'page1'); this.route(/root\/(\d+\S+)\/(\d+\S+)/, 'page2'); }, page0:function(){ console.log("no id"); }, page1:function(id1){ console.log(id1); }, page2:function(id1,id2){ console.log(id1); console.log(id2); } }); 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/958067/


All Articles