Is it possible to have optional parameters in a Backbone.js route?
e.g this:
routes:
\"search/[:query]\": \"searchIndex\"
instead of:
You can add regex based routes manually using the route method:
route
router.route(route, name, [callback])Manually create a route for the router, The
routeargument may be a routing string or regular expression. Each matching capture from the route or regular expression will be passed as an argument to the callback.
So something like this should work:
this.route(/^search\/(.*)?/, 'searchIndex');
Then searchIndex would get called with your nothing or your :query as its argument.
The downside is that you can't put regex routes into your routes object. You could add all your routes with route inside your router's initialize method if you wanted to keep them all together.