My app basically takes some form input and returns a set of results. I have two routes
routes: {
'': 'search',
'search': 'search',
'results/:query': 'results'
},
results: function(query) {
var search = new ResultsSearchView();
var grid = new GridView({ query: query });
}
If the query contains any characters / specifically (can totally happen in this case), they are added to the URL and my route breaks.
I have tried using encodeURI() and encodeURIComponent() bit I’m not having any luck. How are you folks handling such things?
You can use
encodeURIComponentwhen building the URL to convert the/to%2Fand thendecodeURIComponentinside the route handler to convert them back; the HTML would end looking like this:and then in the router:
Demo: http://jsfiddle.net/ambiguous/sbpfD/
Alternatively, you could use a splat route:
So your HTML would be like this:
and your router:
Demo: http://jsfiddle.net/ambiguous/awJxG/