diff options
Diffstat (limited to 'src/service/route.js')
| -rw-r--r-- | src/service/route.js | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/src/service/route.js b/src/service/route.js index 9534968a..e1d0e7be 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -62,7 +62,7 @@ </doc:scenario> </doc:example> */ -angularServiceInject('$route', function(location, $updateView) { +angularServiceInject('$route', function($location, $updateView) { var routes = {}, onChange = [], matcher = switchRouteMatcher, @@ -207,66 +207,67 @@ angularServiceInject('$route', function(location, $updateView) { function updateRoute(){ - var childScope, routeParams, pathParams, segmentMatch, key, redir; + var selectedRoute, pathParams, segmentMatch, key, redir; + if ($route.current && $route.current.scope) { + $route.current.scope.$destroy(); + } $route.current = null; + // Match a route forEach(routes, function(rParams, rPath) { if (!pathParams) { - if (pathParams = matcher(location.hashPath, rPath)) { - routeParams = rParams; + if (pathParams = matcher($location.hashPath, rPath)) { + selectedRoute = rParams; } } }); - // "otherwise" fallback - routeParams = routeParams || routes[null]; + // No route matched; fallback to "otherwise" route + selectedRoute = selectedRoute || routes[null]; - if(routeParams) { - if (routeParams.redirectTo) { - if (isString(routeParams.redirectTo)) { + if(selectedRoute) { + if (selectedRoute.redirectTo) { + if (isString(selectedRoute.redirectTo)) { // interpolate the redirectTo string redir = {hashPath: '', - hashSearch: extend({}, location.hashSearch, pathParams)}; + hashSearch: extend({}, $location.hashSearch, pathParams)}; - forEach(routeParams.redirectTo.split(':'), function(segment, i) { + forEach(selectedRoute.redirectTo.split(':'), function(segment, i) { if (i==0) { redir.hashPath += segment; } else { segmentMatch = segment.match(/(\w+)(.*)/); key = segmentMatch[1]; - redir.hashPath += pathParams[key] || location.hashSearch[key]; + redir.hashPath += pathParams[key] || $location.hashSearch[key]; redir.hashPath += segmentMatch[2] || ''; delete redir.hashSearch[key]; } }); } else { // call custom redirectTo function - redir = {hash: routeParams.redirectTo(pathParams, location.hash, location.hashPath, - location.hashSearch)}; + redir = {hash: selectedRoute.redirectTo(pathParams, $location.hash, $location.hashPath, + $location.hashSearch)}; } - location.update(redir); - $updateView(); //TODO this is to work around the $location<=>$browser issues + $location.update(redir); return; } - childScope = createScope(parentScope); - $route.current = extend({}, routeParams, { - scope: childScope, - params: extend({}, location.hashSearch, pathParams) - }); + $route.current = extend({}, selectedRoute); + $route.current.params = extend({}, $location.hashSearch, pathParams); } //fire onChange callbacks - forEach(onChange, parentScope.$tryEval); + forEach(onChange, parentScope.$eval, parentScope); - if (childScope) { - childScope.$become($route.current.controller); + // Create the scope if we have mtched a route + if ($route.current) { + $route.current.scope = parentScope.$new($route.current.controller); } } - this.$watch(function(){return dirty + location.hash;}, updateRoute); + this.$watch(function(){return dirty + $location.hash;}, updateRoute)(); return $route; }, ['$location', '$updateView']); |
