aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgor Minar2011-01-31 23:26:10 -0800
committerIgor Minar2011-02-01 09:35:19 -0800
commit6c0cf17404e8e6de0c398fff8e71497f39090408 (patch)
treee77f95e0d9d7749c813e410f4bb0bc3b0346733f /src
parentc648fee5c2c46cbd2ea8b5bd4cec8005f182db1c (diff)
downloadangular.js-6c0cf17404e8e6de0c398fff8e71497f39090408.tar.bz2
add redirection support to $route
Closes #217
Diffstat (limited to 'src')
-rw-r--r--src/services.js49
1 files changed, 32 insertions, 17 deletions
diff --git a/src/services.js b/src/services.js
index 395c1298..d6a7fb0a 100644
--- a/src/services.js
+++ b/src/services.js
@@ -690,6 +690,18 @@ angularServiceInject('$route', function(location) {
* @param {string} path Route path (matched against `$location.hash`)
* @param {Object} params Mapping information to be assigned to `$route.current` on route
* match.
+ *
+ * Object properties:
+ *
+ * - `controller` – `{function()=}` – Controller fn that should be associated with newly
+ * created scope.
+ * - `template` – `{string=}` – path to an html template that should be used by
+ * {@link angular.widget.ng:view ng:view} or
+ * {@link angular.widget.ng:include ng:include} widgets.
+ * - `redirectTo` – {string=} – value to update
+ * {@link angular.service.$location $location} hash with and trigger route
+ * redirection.
+ *
* @returns {Object} route object
*
* @description
@@ -721,28 +733,31 @@ angularServiceInject('$route', function(location) {
}
};
function updateRoute(){
- var childScope;
+ var childScope, routeParams, pathParams;
+
$route.current = _null;
- forEach(routes, function(routeParams, route) {
- if (!childScope) {
- var pathParams = matcher(location.hashPath, route);
- if (pathParams) {
- childScope = createScope(parentScope);
- $route.current = extend({}, routeParams, {
- scope: childScope,
- params: extend({}, location.hashSearch, pathParams)
- });
+ forEach(routes, function(rParams, rPath) {
+ if (!pathParams) {
+ if (pathParams = matcher(location.hashPath, rPath)) {
+ routeParams = rParams;
}
}
});
- //fallback
- if (!childScope && routes[_null]) {
- childScope = createScope(parentScope);
- $route.current = extend({}, routes[_null], {
- scope: childScope,
- params: extend({}, location.hashSearch)
- });
+ // "otherwise" fallback
+ routeParams = routeParams || routes[_null];
+
+ if(routeParams) {
+ if (routeParams.redirectTo) {
+ location.updateHash(routeParams.redirectTo); //let $browser trigger next route change
+ return;
+ } else {
+ childScope = createScope(parentScope);
+ $route.current = extend({}, routeParams, {
+ scope: childScope,
+ params: extend({}, location.hashSearch, pathParams)
+ });
+ }
}
//fire onChange callbacks