Having a route like 'dogs': 'process', I need to rewrite it to 'animals': 'process'.
Now, I need the router to recognize both routes, but always display the url like /animals, it is sort of aliasing, but could not find any info on how to solve this without placing an url redirect in 'process' handler.
I'm assuming that the real need for aliases is different than
dogstoanimals, so I'll answer regardless of if the use-case here is good or not. But if you don't want to change the hash but want to trigger different behaviors in the app, using the router is probably not the route to go.Route aliases don't really exist in Backbone, other than defining different routes using the same callback. Depending on your exact use-case, there are multiple ways to handle similar routes.
Replace the hash
To display the same hash for a generic route coming from different routes, use the
replaceoption of thenavigatefunction.then handle the animals route, regardless of which route was initially used to get in this callback.
Some other answers or tutorials will say to use
window.location.hashbut don't. Manually resetting the hash will trigger the route regardless and may cause more trouble than it'll help.Different behaviors but showing the same route
Just use different callbacks, both using the replace trick above.
Notice the inexistent
animalsRoute. You could add the route if there's a generic behavior if no specific animal is chosen.Use the route params
If you want to know which animal was chosen but still use the same callback and remove the chosen animal from the hash, use the route params.
Redirect to the generic route
If you want a different behavior but always show the same route, use different callbacks, then redirect. This is useful if the generic route is in another router instance.
Using the
triggeroptions will call theanimalsRoutein the other router and thereplaceoption will avoid making an entry in the history, so pushing the back button won't go tolionsto get back toanimalsand being caught in the animals route.