Is it possible to have optional parameters in a Backbone.js route?
e.g this:
routes:
"search/[:query]": "searchIndex"
instead of:
routes:
"search/": "searchIndex"
"search/:query": "searchIndex"
As of Backbone 0.9.9, you can add optional paramaters with parentheses.
For example in your routes object you can define an optional route part like this:
routes: {
"organize(/:action)": "displayOrganize"
}
Now the url path will match /#organize
and routes like /#organize/create
.
Keep in mind that if you need routes like /#organize/
(with a trailing slash) to be recognized, you can do:
routes: {
"organize(/)(:action)": "displayOrganize"
}
Probably the most easiest way is just declare more than one route, one with the extra arg,one without:
routes:{
"authProxy/:hash": "authProxy",
"authProxy/:hash/:url": "authProxy"
}
then just check for them in your method:
authProxy: function(hash, url){
if (url){
// Hash and URL.
}else{
// Just hash.
}
}
Note that I like this much better than the other two answers because it's very easy for another developer to understand what's going on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With