Search code examples
javascriptbackbone.jsrouterbackbone-routing

backbone router several root, how to load this properly?


I'm missing something here. My problem is very simple I think. mysite.com/app and mysite.com/transaction both load a different html page but they load the same router.

For each case I need my router to be written in order than mysite.com/app/#/someparams triggers 'app' function mysite.com/transaction/#/someparams triggers 'transaction' function. (With or without # I don't care but this is the way it works for me =/ )

If I write :

routes: {
    'app':'app',
    'app/:params':'app',
    'transaction':'transaction',
    'transaction/:params': 'transaction'
}

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

Then mysite.com/app/#/params or mysite.com/transaction/#/params won't trigger the wanted functions.

If I write this:

     Backbone.history.start({
        pushState: false,
        root:'/app'
    });

then parameters will be ok for /app but not for /transaction. What to do ? I'm pretty sure I'm missing something simple but I'm kinda confused right now ..

Thanks for your help


Solution

  • Your route is only expecting a single parameter in both the routes that you have defined.

    mysite.com/app/#/params matches app/:params The # is being passed in as a parameter here.

    Also the hash should be preceding the route map.

    mysite.com#app/params This would match the route expected by backbone as per your map.

    You could just use 2 routes using the optional feature.

    routes: {
        'app(/:params)':'app',
        'transaction(/:params)':'transaction'
    }