Skip to content

Commit

Permalink
fix($route): update $route to reflect $location changes
Browse files Browse the repository at this point in the history
* update $route to reflect new $location
* add some more unit tests to $route
* fix some other failing unit tests
* redirect overrides the url now

Breaks $route custom redirect fn has only 3 params now
  • Loading branch information
vojtajina committed Sep 8, 2011
1 parent 5ba227c commit 22cb600
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 187 deletions.
39 changes: 19 additions & 20 deletions src/service/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @property {Array.<Object>} routes Array of all configured routes.
*
* @description
* Watches `$location.hashPath` and tries to map the hash to an existing route
* Watches `$location.url()` and tries to map the path to an existing route
* definition. It is used for deep-linking URLs to controllers and views (HTML partials).
*
* The `$route` service is typically used in conjunction with {@link angular.widget.ng:view ng:view}
Expand All @@ -20,7 +20,6 @@
* @example
This example shows how changing the URL hash causes the <tt>$route</tt>
to match a route against the URL, and the <tt>[[ng:include]]</tt> pulls in the partial.
Try changing the URL in the input box to see changes.
<doc:example>
<doc:source jsfiddle="false">
Expand Down Expand Up @@ -51,7 +50,7 @@
<a href="#/Book/Moby/ch/1">Moby: Ch1</a> |
<a href="#/Book/Gatsby">Gatsby</a> |
<a href="#/Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a><br/>
$location.hashPath: <input type="text" name="$location.hashPath" size="80" />
<pre>$location.path() = {{$location.path()}}</pre>
<pre>$route.current.template = {{$route.current.template}}</pre>
<pre>$route.current.params = {{$route.current.params}}</pre>
<pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
Expand Down Expand Up @@ -159,21 +158,20 @@ angularServiceInject('$route', function($location, $routeParams) {
* {@link angular.widget.ng:view ng:view} or
* {@link angular.widget.ng:include ng:include} widgets.
* - `redirectTo` – {(string|function())=} – value to update
* {@link angular.service.$location $location} hash with and trigger route redirection.
* {@link angular.service.$location $location} path with and trigger route redirection.
*
* If `redirectTo` is a function, it will be called with the following parameters:
*
* - `{Object.<string>}` - route parameters extracted from the current
* `$location.hashPath` by applying the current route template.
* - `{string}` - current `$location.hash`
* - `{string}` - current `$location.hashPath`
* - `{string}` - current `$location.hashSearch`
* `$location.path()` by applying the current route template.
* - `{string}` - current `$location.path()`
* - `{Object}` - current `$location.search()`
*
* The custom `redirectTo` function is expected to return a string which will be used
* to update `$location.hash`.
* to update `$location.path()` and `$location.search()`.
*
* - `[reloadOnSearch=true]` - {boolean=} - reload route when $location.hashSearch
* changes.
* - `[reloadOnSearch=true]` - {boolean=} - reload route when only $location.search()
* changes.
*
* If the option is set to false and url in the browser changes, then
* $routeUpdate event is emited on the current route scope. You can use this event to
Expand Down Expand Up @@ -231,9 +229,7 @@ angularServiceInject('$route', function($location, $routeParams) {
}
};



this.$watch(function(){ return dirty + $location.hash; }, updateRoute);
this.$watch(function() { return dirty + $location.url(); }, updateRoute);

return $route;

Expand Down Expand Up @@ -278,10 +274,13 @@ angularServiceInject('$route', function($location, $routeParams) {
$route.current = next;
if (next) {
if (next.redirectTo) {
$location.update(isString(next.redirectTo)
? {hashSearch: next.params, hashPath: interpolate(next.redirectTo, next.params)}
: {hash: next.redirectTo(next.pathParams,
$location.hash, $location.hashPath, $location.hashSearch)});
if (isString(next.redirectTo)) {
$location.path(interpolate(next.redirectTo, next.params)).search(next.params)
.replace();
} else {
$location.url(next.redirectTo(next.pathParams, $location.path(), $location.search()))
.replace();
}
} else {
copy(next.params, $routeParams);
next.scope = parentScope.$new(next.controller);
Expand All @@ -299,9 +298,9 @@ angularServiceInject('$route', function($location, $routeParams) {
// Match a route
var params, match;
forEach(routes, function(route, path) {
if (!match && (params = matcher($location.hashPath, path))) {
if (!match && (params = matcher($location.path(), path))) {
match = inherit(route, {
params: extend({}, $location.hashSearch, params),
params: extend({}, $location.search(), params),
pathParams: params});
match.$route = route;
}
Expand Down
4 changes: 2 additions & 2 deletions src/service/routeParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*
* @description
* Current set of route parameters. The route parameters are a combination of the
* {@link angular.service.$location $location} `hashSearch`, and `path`. The `path` parameters
* {@link angular.service.$location $location} `search()`, and `path()`. The `path` parameters
* are extracted when the {@link angular.service.$route $route} path is matched.
*
* In case of parameter name collision, `path` params take precedence over `hashSearch` params.
* In case of parameter name collision, `path` params take precedence over `search` params.
*
* The service guarantees that the identity of the `$routeParams` object will remain unchanged
* (but its properties will likely change) even when a route change occurs.
Expand Down
13 changes: 0 additions & 13 deletions test/ScenarioSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,4 @@ describe("ScenarioSpec: Compilation", function(){
expect(jqLite(scope.$element).text()).toEqual('123');
});
});

describe("configuration", function(){
it("should take location object", function(){
var url = "http://server/#?book=moby";
scope = angular.compile("<div>{{$location}}</div>")();
var $location = scope.$service('$location');
var $browser = scope.$service('$browser');
expect($location.hashSearch.book).toBeUndefined();
$browser.url(url);
$browser.poll();
expect($location.hashSearch.book).toEqual('moby');
});
});
});
8 changes: 4 additions & 4 deletions test/service/routeParamsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ describe('$routeParams', function(){
$route.when('/foo');
$route.when('/bar/:barId');

$location.hash = '/foo?a=b';
$location.path('/foo').search('a=b');
scope.$digest();
expect($routeParams).toEqual({a:'b'});

$location.hash = '/bar/123?x=abc';
$location.path('/bar/123').search('x=abc');
scope.$digest();
expect($routeParams).toEqual({barId:'123', x:'abc'});
});
Expand All @@ -30,11 +30,11 @@ describe('$routeParams', function(){
$route.when('/foo');
$route.when('/bar/:barId');

$location.hash = '/foo?a=b';
$location.path('/foo').search('a=b');
scope.$digest();
expect(scope.$service('$routeParams')).toBe(firstRouteParams);

$location.hash = '/bar/123?x=abc';
$location.path('/bar/123').search('x=abc');
scope.$digest();
expect(scope.$service('$routeParams')).toBe(firstRouteParams);
});
Expand Down
Loading

0 comments on commit 22cb600

Please sign in to comment.