Search code examples
javascriptbackbone.jsurl-routing

Backbonejs routing casesensitivity issue


I am using backbonejs router and it seems that routing is not working for opposite case of route which has been configured.

    var AppRouter = Backbone.Router.extend({
    routes: {
        'project(/)(/index)': 'showProjects',
        'project/:id': 'showProjectEdit'
    }
});

Works for http://www.test.com:53895/project But not working when P is in uppercase http://www.test.com:53895/Project

The version which i am using is 1.1.2. Please help!


Solution

  • We can make the route to be case insensitive by adding the 'i' attribute to the return value of _routeToRegExp function in backbonejs library.

    _routeToRegExp: function (route) {
          var namedParam    = /:\w+/g;
          var splatParam    = /\*\w+/g;
          var escapeRegExp  = /[-[\]{}()+?.,\\^$|#\s]/g;    
    
          route = route.replace(escapeRegExp, '\\$&')
                       .replace(namedParam, '([^\/]+\/?)')
                       .replace(splatParam, '(.*?)');
    
          return new RegExp('^' + route + '$');
          /*
           * Note: If you would like case insensitivity, 
           *       add the "i" attribute to the return
           * return new RegExp('^' + route + '$', 'i');
           */
    }
    

    https://github.com/jashkenas/backbone/issues/848

    Thanks Dave!