Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit daec562

Browse files
committed
fixup! feat($route): add support for the reloadOnUrl configuration option
1 parent d18e5f0 commit daec562

File tree

2 files changed

+63
-52
lines changed

2 files changed

+63
-52
lines changed

src/ngRoute/route.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ function $RouteProvider() {
196196
* If the option is set to `false` and the URL in the browser changes, then a `$routeUpdate`
197197
* event is broadcasted on the root scope (without reloading the route).
198198
*
199-
* **Note:** This option has no effect if `reloadOnUrl` is set to false.
199+
* <div class="alert alert-warning">
200+
* **Note:** This option has no effect if `reloadOnUrl` is set to `false`.
201+
* </div>
200202
*
201203
* - `[caseInsensitiveMatch=false]` - `{boolean=}` - match routes without being case sensitive
202204
*
@@ -556,9 +558,9 @@ function $RouteProvider() {
556558
* @name $route#$routeUpdate
557559
* @eventType broadcast on root scope
558560
* @description
559-
* Any of the `reloadOnSearch` and `reloadOnUrl` properties has been set to false and we are
560-
* reusing the same instance of the route (including template, controller instance, resolved
561-
* dependencies etc).
561+
* Broadcasted if the same instance of a route (including template, controller instance,
562+
* resolved dependencies, etc.) is being reused. This can happen if either `reloadOnSearch` or
563+
* `reloadOnUrl` has been set to `false`.
562564
*
563565
* @param {Object} angularEvent Synthetic event object
564566
* @param {Route} current Current/previous route information.
@@ -666,21 +668,7 @@ function $RouteProvider() {
666668
var lastRoute = $route.current;
667669

668670
preparedRoute = parseRoute();
669-
preparedRouteIsUpdateOnly =
670-
// IF this is not a forced reload
671-
!forceReload
672-
// AND both `lastRoute`/`preparedRoute` are defined
673-
&& preparedRoute && lastRoute
674-
// AND they map to the same Route Definition Object
675-
&& (preparedRoute.$$route === lastRoute.$$route)
676-
// AND `reloadOnUrl` is disabled
677-
&& (!preparedRoute.reloadOnUrl
678-
// OR `reloadOnSearch` is disabled
679-
|| (!preparedRoute.reloadOnSearch
680-
// AND both routes have the same path params
681-
&& angular.equals(preparedRoute.pathParams, lastRoute.pathParams)
682-
)
683-
);
671+
preparedRouteIsUpdateOnly = isNavigationUpdateOnly(preparedRoute, lastRoute);
684672

685673
if (!preparedRouteIsUpdateOnly && (lastRoute || preparedRoute)) {
686674
if ($rootScope.$broadcast('$routeChangeStart', preparedRoute, lastRoute).defaultPrevented) {
@@ -860,6 +848,29 @@ function $RouteProvider() {
860848
return match || routes[null] && inherit(routes[null], {params: {}, pathParams:{}});
861849
}
862850

851+
/**
852+
* @param {Object} newRoute - The new route configuration (as returned by `parseRoute()`).
853+
* @param {Object} oldRoute - The previous route configuration (as returned by `parseRoute()`).
854+
* @returns {boolean} Whether this is an "update-only" navigation, i.e. the URL maps to the same
855+
* route and it can be reused (based on the config and the type of change).
856+
*/
857+
function isNavigationUpdateOnly(newRoute, oldRoute) {
858+
// IF this is not a forced reload
859+
return !forceReload
860+
// AND both `newRoute`/`oldRoute` are defined
861+
&& newRoute && oldRoute
862+
// AND they map to the same Route Definition Object
863+
&& (newRoute.$$route === oldRoute.$$route)
864+
// AND `reloadOnUrl` is disabled
865+
&& (!newRoute.reloadOnUrl
866+
// OR `reloadOnSearch` is disabled
867+
|| (!newRoute.reloadOnSearch
868+
// AND both routes have the same path params
869+
&& angular.equals(newRoute.pathParams, oldRoute.pathParams)
870+
)
871+
);
872+
}
873+
863874
/**
864875
* @returns {string} interpolation of the redirect path with the parameters
865876
*/

test/ngRoute/routeSpec.js

+33-33
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ describe('$route', function() {
16791679

16801680

16811681
describe('reloadOnUrl', function() {
1682-
it('should reload a route when `reloadOnUrl` is enabled and `.url()` changes', function() {
1682+
it('should reload when `reloadOnUrl` is true and `.url()` changes', function() {
16831683
var routeChange = jasmine.createSpy('routeChange');
16841684

16851685
module(function($routeProvider) {
@@ -1722,7 +1722,7 @@ describe('$route', function() {
17221722
});
17231723

17241724

1725-
it('should reload a route when `reloadOnUrl` is disabled and URL maps to different route',
1725+
it('should reload when `reloadOnUrl` is false and URL maps to different route',
17261726
function() {
17271727
var routeChange = jasmine.createSpy('routeChange');
17281728
var routeUpdate = jasmine.createSpy('routeUpdate');
@@ -1760,7 +1760,7 @@ describe('$route', function() {
17601760
);
17611761

17621762

1763-
it('should not reload a route when `reloadOnUrl` is disabled and URL maps to the same route',
1763+
it('should not reload when `reloadOnUrl` is false and URL maps to the same route',
17641764
function() {
17651765
var routeChange = jasmine.createSpy('routeChange');
17661766
var routeUpdate = jasmine.createSpy('routeUpdate');
@@ -1826,7 +1826,7 @@ describe('$route', function() {
18261826
});
18271827

18281828

1829-
describe('reload', function() {
1829+
describe('with `$route.reload()`', function() {
18301830
var $location;
18311831
var $log;
18321832
var $rootScope;
@@ -1940,7 +1940,7 @@ describe('$route', function() {
19401940
});
19411941

19421942
describe('reloadOnSearch', function() {
1943-
it('should not have no effect if `reloadOnUrl` is set to `false`', function() {
1943+
it('should not have any effect if `reloadOnUrl` is false', function() {
19441944
var reloaded = jasmine.createSpy('route reload');
19451945

19461946
module(function($routeProvider) {
@@ -1975,7 +1975,7 @@ describe('$route', function() {
19751975
});
19761976

19771977

1978-
it('should reload a route when reloadOnSearch is enabled and .search() or .hash() changes',
1978+
it('should reload when `reloadOnSearch` is true and `.search()`/`.hash()` changes',
19791979
function() {
19801980
var reloaded = jasmine.createSpy('route reload');
19811981

@@ -2011,7 +2011,7 @@ describe('$route', function() {
20112011
);
20122012

20132013

2014-
it('should not reload a route when reloadOnSearch is disabled and .search() or .hash() changes',
2014+
it('should not reload when `reloadOnSearch` is false and `.search()`/`.hash()` changes',
20152015
function() {
20162016
var routeChange = jasmine.createSpy('route change'),
20172017
routeUpdate = jasmine.createSpy('route update');
@@ -2052,39 +2052,39 @@ describe('$route', function() {
20522052
);
20532053

20542054

2055-
it('should reload reloadOnSearch route when url differs only in route path param', function() {
2056-
var routeChange = jasmine.createSpy('route change');
2055+
it('should reload when `reloadOnSearch` is true and url differs only in route path param',
2056+
function() {
2057+
var routeChange = jasmine.createSpy('route change');
20572058

2058-
module(function($routeProvider) {
2059-
$routeProvider.when('/foo/:fooId', {controller: angular.noop, reloadOnSearch: false});
2060-
});
2059+
module(function($routeProvider) {
2060+
$routeProvider.when('/foo/:fooId', {controller: angular.noop, reloadOnSearch: false});
2061+
});
20612062

2062-
inject(function($route, $location, $rootScope) {
2063-
$rootScope.$on('$routeChangeStart', routeChange);
2064-
$rootScope.$on('$routeChangeSuccess', routeChange);
2063+
inject(function($route, $location, $rootScope) {
2064+
$rootScope.$on('$routeChangeStart', routeChange);
2065+
$rootScope.$on('$routeChangeSuccess', routeChange);
20652066

2066-
expect(routeChange).not.toHaveBeenCalled();
2067+
expect(routeChange).not.toHaveBeenCalled();
20672068

2068-
$location.path('/foo/aaa');
2069-
$rootScope.$digest();
2070-
expect(routeChange).toHaveBeenCalled();
2071-
expect(routeChange).toHaveBeenCalledTimes(2);
2072-
routeChange.calls.reset();
2069+
$location.path('/foo/aaa');
2070+
$rootScope.$digest();
2071+
expect(routeChange).toHaveBeenCalledTimes(2);
2072+
routeChange.calls.reset();
20732073

2074-
$location.path('/foo/bbb');
2075-
$rootScope.$digest();
2076-
expect(routeChange).toHaveBeenCalled();
2077-
expect(routeChange).toHaveBeenCalledTimes(2);
2078-
routeChange.calls.reset();
2074+
$location.path('/foo/bbb');
2075+
$rootScope.$digest();
2076+
expect(routeChange).toHaveBeenCalledTimes(2);
2077+
routeChange.calls.reset();
20792078

2080-
$location.search({foo: 'bar'}).hash('baz');
2081-
$rootScope.$digest();
2082-
expect(routeChange).not.toHaveBeenCalled();
2083-
});
2084-
});
2079+
$location.search({foo: 'bar'}).hash('baz');
2080+
$rootScope.$digest();
2081+
expect(routeChange).not.toHaveBeenCalled();
2082+
});
2083+
}
2084+
);
20852085

20862086

2087-
it('should update params when reloadOnSearch is disabled and .search() changes', function() {
2087+
it('should update params when `reloadOnSearch` is false and `.search()` changes', function() {
20882088
var routeParamsWatcher = jasmine.createSpy('routeParamsWatcher');
20892089

20902090
module(function($routeProvider) {
@@ -2172,7 +2172,7 @@ describe('$route', function() {
21722172
});
21732173

21742174

2175-
describe('reload', function() {
2175+
describe('with `$route.reload()`', function() {
21762176
var $location;
21772177
var $log;
21782178
var $rootScope;

0 commit comments

Comments
 (0)