Reputation: 65540
I'm trying to set up routing in Backbone 0.9.10. I'd like to match routes of the following kind:
/england/
/england/birmingham
/france
/france/paris
...
etc. This is what I have in my router at the moment:
var AppRouter = Backbone.Router.extend({
routes: {
"": "index",
"(/:country)": "index",
"(/:country)(/:city)": "index"
},
index: function(country, city) {
console.log('index', country, city);
}
});
var StateApp = new AppRouter();
Backbone.history.start({ pushState: true });
I have two problems:
/
, /england
or anything else. country
parameter to be a parameter, rather than specifying individual countries.I'd much rather use proper URL routing than regex parsing if possible.
Upvotes: 5
Views: 6862
Reputation: 2174
If you want to have a single route and have flexibility to change what you want the urls to look like. you could also consider
routes: {
"(/country/:country)(/city/:city)": "index"
}
matching
"" (Empty string)
"country/england"
"country/england/city/london"
Upvotes: 2
Reputation: 35920
If you want, as in your example, to route all urls (including the root) to one method, your only need to define one route:
routes: {
"(:country)(/:city)": "index"
}
Because both parameters are optional, this will match:
If you want only the routes in format of england
and england/london
but not the root page /
, declare a separate empty route, and make the :country
part non-optional:
routes: {
"" : "home",
":country(/:city)": "index"
}
Upvotes: 14