Does anyone know how to make the Backbone routes case insensitive?
I.E.
http://localhost/Products
http://localhost/products
both trigger the products route
routes: {
"products": "products"
},
Update
Based on mu is too short’s answer, here is the full Router. Thanks for your help
MainRouter = Backbone.Router.extend({
routes: {
"": "home",
"products": "products"
},
initialize: function () {
Backbone.history.start({ pushState: true });
},
home: function () {
// just show the products!!
this.products();
},
products: function () {
// wire up the view.
var ps = new ProductsView({ el: '#products', collection: myCollection });
ps.render();
// fetch the collection!!
myCollection.fetch();
},
_routeToRegExp: function (route) {
route = route.replace(this.escapeRegExp, "\\$&")
.replace(this.namedParam, "([^\/]*)")
.replace(this.splatParam, "(.*?)");
return new RegExp('^' + route + '$', 'i'); // Just add the 'i'
}
});
You could bind all your routes manually with
Backbone.Router.route(), that will accept the route as a string or a RegExp object:Or you could replace this private method in
Backbone.Routerwhile subclassing (viaBackbone.Router.extend(), thank you Crescent Fresh):with this one (you’ll have to copy/expand the
escapeRegExpnamedParam, andsplatParamregexes too):The routes in the
routesobject are added to the routing table usingBackbone.Router.route()and that converts them to regexes using_routeToRegExp.Here’s a demo of the
_routeToRegExpapproach: http://jsfiddle.net/ambiguous/MDSC5/