0

I have a backbone.js router set up with some routes as follows :

routes : {
  'a-route' : 'goToRoute',
  'a-route/*splat' : 'goToRoute'
}

goToRoute : function(splat){
if(!splat) {
  // do this
} else {
  // do that with splat
}

When I do a

router.navigate('a-route', {trigger : true});

everything works just fine. But when I do

router.navigate('a-route/more', {trigger : true});

the router is firing twice : first with the splat equal to 'undefined', and then a second time with the splat equal to 'more'.

If I comment out the route 'a-route' : 'goToRoute', then everything works as it should with router.navigate('a-route/more') ... but I need both routes - with and without the splat.

According to the docs I think I have this set up correctly, any ideas?

Petrov
  • 4,200
  • 6
  • 40
  • 61
  • What's wrong with what you have already? just redirect from `splat === undefined` to wherever you want to go from there. – jakee Jul 03 '12 at 09:36
  • there is a different layout depending on whether there splat === 'more' or splat === 'undefined'. Currently, calling router.navigate('a-route/more', {trigger:true}) is triggering both routes, and so it is rendering both layouts on the same page – Petrov Jul 03 '12 at 10:11
  • i meant, remove the `a-route: goToRoute` and in `goToRoute` do `if (splat === undefined) {justARoute(); } else { splatRoute(splat); }` – jakee Jul 03 '12 at 10:29
  • if i do it this way, a call to router.navigate('a-route') doesn't find a match - the page is not rendered – Petrov Jul 03 '12 at 10:44
  • what about `'a-route': 'goToRouteWithoutSplat'` and `'a-route/*splat': goToRouteWithSplat`. The splat = undefined is caused by your routing working completely fine, but when you go to the route 'a-route', there is not parameter relayed to the route function – jakee Jul 03 '12 at 10:49
  • 1
    I may have the wrong setup, but works for me http://jsfiddle.net/7u2Bg/1/ with or without pushstate – nikoshr Jul 03 '12 at 11:45
  • thanks @nikoshr that's the right setup... perhaps the problem is somewhere else – Petrov Jul 03 '12 at 11:53

1 Answers1

0

You do not need to use *, you must use : in your case.

routes : {
  'a-route' : 'goToRoute',
  'a-route/:splat' : 'goToRoute'
}
Daniel Aranda
  • 6,426
  • 2
  • 21
  • 28
  • I may be wrong, but I don't think there is a difference between a parameter and a splat if there is only one item after the backslash. I think a splat just lets you put everything after the backslash into a single variable, whereas parameters each represent one component. – Petrov Jul 03 '12 at 16:56
  • If you try in the way that I paste you it will enter only one time. The problem with splat is that is some kind of wildcard so your router has two valid entries. – Daniel Aranda Jul 03 '12 at 17:18
  • the other possibility is that you have a call to router.navigate('a-route', {trigger : true}); inside the function that is called on 'a-route/*splat' – Daniel Aranda Jul 03 '12 at 17:22