durandaldurandal-2.0durandal-navigation

How to combine optional parameter route with static route on the same name (Durandal router)


I have working router configured inside shell.js:

router.map([
  { route: 'messages', moduleId: 'views/messages', nav: true },
  { route: 'messages/create', moduleId: 'views/messagesCreate', nav: true },
]).buildNavigationModel();

What i want to do is to let my #messages route able to retrieve optional parameter id (/:id) something like this:

router.map([
      { route: 'messages(/:id)', moduleId: 'views/messages', nav: true },
      { route: 'messages/create', moduleId: 'views/messagesCreate', nav: true },
    ]).buildNavigationModel();

so i can navigate to messages page with or without parameter..

this is work fine, but the problem come when i want to navigate to messages/create page..

How can i accomplished this by without need to change my route name ?

Any help, idea or suggestions will greatly appreciated!
Thanks


Solution

  • Change the order of your routes

    router.map([
      { route: 'messages/create', moduleId: 'views/messagesCreate', nav: true },
      { route: 'messages(/:id)', moduleId: 'views/messages', nav: true },
    ]).buildNavigationModel();