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

$route service should reload controllers only if hashPath changes #329

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/service/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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.hash` and tries to map the hash 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 Down Expand Up @@ -62,6 +62,7 @@ angularServiceInject('$route', function(location, $updateView) {
matcher = switchRouteMatcher,
parentScope = this,
dirty = 0,
locationWatchProp = 'hash',
$route = {
routes: routes,

Expand Down Expand Up @@ -172,6 +173,20 @@ angularServiceInject('$route', function(location, $updateView) {
*/
reload: function() {
dirty++;
},

/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#watchHashPathOnly
* @methodOf angular.service.$route
*
* @description
* Causes `$route` service to be reload only if `$location.hashPath` changes. Other properties of
* {@link angular.service.$location $location service} won't trigger reloads.
*/
watchHashPathOnly: function(hashPathOnly) {
locationWatchProp = hashPathOnly ? 'hashPath' : 'hash';
}
};

Expand Down Expand Up @@ -260,7 +275,7 @@ angularServiceInject('$route', function(location, $updateView) {
}


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

return $route;
}, ['$location', '$updateView']);
38 changes: 37 additions & 1 deletion test/service/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('$route', function() {
});


it('should route and fire change event', function(){
it('should change route and fire change event', function(){
var log = '',
$location, $route;

Expand Down Expand Up @@ -52,6 +52,42 @@ describe('$route', function() {
});


it('should not reload route when only hashSearch changes, but hashPath does not', function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$route = scope.$service('$route'),
log = [];

$route.when('/foo', {controller: FooCtrl});

function FooCtrl() {
log.push('foo');
}

expect(log).toEqual([]);

//without watchHashPathOnly setting the route should always reload
$location.hashPath = '/foo';
scope.$eval();
$location.hashSearch.foo = 'bar';
scope.$eval();
expect(log).toEqual(['foo', 'foo']);

//reset log and change $route config
log = [];
$route.watchHashPathOnly(true);

//with watchHashPathOnly on, only hashPath changes should not trigger reload
$location.hashPath = '/foo';
scope.$eval();
expect(log).toEqual(['foo']);

$location.hashSearch.foo = 'bar';
scope.$eval();
expect(log).toEqual(['foo']);
});


it('should return fn registered with onChange()', function() {
var scope = angular.scope(),
$route = scope.$service('$route'),
Expand Down