Added support for trailing slashes in routes

m.route now matches a path with a trailing slash against a route without one.
This commit is contained in:
Sebastian 2014-05-22 14:41:21 +02:00
parent 67c5400d44
commit d10d77288c
2 changed files with 17 additions and 2 deletions

View file

@ -310,11 +310,11 @@ Mithril = m = new function app(window) {
for (var route in router) {
if (route == path) return !void m.module(root, router[route])
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "$")
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
if (matcher.test(path)) {
return !void path.replace(matcher, function() {
var keys = route.match(/:[^\/]+/g)
var keys = route.match(/:[^\/]+/g) || []
var values = [].slice.call(arguments, 1, -2)
for (var i = 0; i < keys.length; i++) routeParams[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
m.module(root, router[route])