aboutsummaryrefslogtreecommitdiffstats
path: root/src/service/route.js
diff options
context:
space:
mode:
authorIgor Minar2012-03-20 00:38:08 -0700
committerIgor Minar2012-03-20 11:07:37 -0700
commita4fe51da3ba0dc297ecd389e230d6664f250c9a6 (patch)
tree68f86adad082bcf23d96c2b71d47a37060841a18 /src/service/route.js
parentee5a5352fd4b94cedee6ef20d4bf2d43ce77e00b (diff)
downloadangular.js-a4fe51da3ba0dc297ecd389e230d6664f250c9a6.tar.bz2
feat($route): when matching consider trailing slash as optional
This makes for a much more flexible route matching: - route /foo matches /foo and redirects /foo/ to /foo - route /bar/ matches /bar/ and redirects /bar to /bar/ Closes #784
Diffstat (limited to 'src/service/route.js')
-rw-r--r--src/service/route.js15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/service/route.js b/src/service/route.js
index 9748de69..2b9d187a 100644
--- a/src/service/route.js
+++ b/src/service/route.js
@@ -18,7 +18,10 @@ function $RouteProvider(){
* @name angular.module.ng.$routeProvider#when
* @methodOf angular.module.ng.$routeProvider
*
- * @param {string} path Route path (matched against `$location.hash`)
+ * @param {string} path Route path (matched against `$location.path`). If `$location.path`
+ * contains redudant trailing slash or is missing one, the route will still match and the
+ * `$location.path` will be updated to add or drop the trailing slash to exacly match the
+ * route definition.
* @param {Object} route Mapping information to be assigned to `$route.current` on route
* match.
*
@@ -57,6 +60,16 @@ function $RouteProvider(){
var routeDef = routes[path];
if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true};
if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions?
+
+ // create redirection for trailing slashes
+ if (path) {
+ var redirectPath = (path[path.length-1] == '/')
+ ? path.substr(0, path.length-1)
+ : path +'/';
+
+ routes[redirectPath] = {redirectTo: path};
+ }
+
return routeDef;
};