Search code examples
javascriptbackbone.jsbackbone-routing

Backbone.js router url contains parameter


How can i match route if the url contains somewhere a parameter with a specified action ? Like myaction/someparam1/someparam2/PleaseMatchMeIfYourFindMe/someparam3


Solution

  • Backbone's routes objects don't offer quite that level of flexibility. However, you can add routing regexes manually using the route method:

    route router.route(route, name, [callback])

    Manually create a route for the router, The route argument may be a routing string or regular expression.

    So you can do things like this in your router:

    initialize: function() {
        this.route(/^myaction\/.*PleaseMatchMeIfYourFindMe/, 'handler_method');
    }
    

    You will of course have to adjust the regex to match your real requirements. If you want to get parts of the regex as arguments to your handler then add capture groups as needed.

    Demo: http://jsfiddle.net/ambiguous/pFXr6/