-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngRoute): add method for changing url params #8358
Changes from 1 commit
fb0cc84
1ef47e3
06f9a82
fd6b3b8
fa11e33
93b8e5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,6 +436,21 @@ function $RouteProvider(){ | |
reload: function() { | ||
forceReload = true; | ||
$rootScope.$evalAsync(updateRoute); | ||
}, | ||
|
||
/** | ||
* @ngdoc method | ||
* @name $route#update | ||
* | ||
* @description | ||
* Causes `$route` service to update the current URL, replacing | ||
* current route parameters with those specified in `newParams`. | ||
* | ||
* @param {Object} newParams mapping of URL parameter names to values | ||
*/ | ||
update: function(newParams) { | ||
newParams = defaults(newParams, this.current.params); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the |
||
$location.path(interpolate(this.current.$$route.originalPath, newParams)); | ||
} | ||
}; | ||
|
||
|
@@ -589,5 +604,22 @@ function $RouteProvider(){ | |
}); | ||
return result.join(''); | ||
} | ||
|
||
/** | ||
* @returns {Object} object composed of all keys in `object` and `defaults`, | ||
* where keys missing in `object` have the value from the | ||
* same key in `defaults` | ||
*/ | ||
function defaults(object, other) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
var o = angular.copy(object); | ||
|
||
for (var key in other) { | ||
if (other.hasOwnProperty(key)) { | ||
o[key] = o[key] || other[key]; | ||
} | ||
} | ||
|
||
return o; | ||
} | ||
}]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1046,6 +1046,73 @@ describe('$route', function() { | |
}); | ||
}); | ||
|
||
describe('update', function() { | ||
it('should support single-parameter route updating', function() { | ||
var routeChangeSpy = jasmine.createSpy('route change'); | ||
|
||
module(function($routeProvider) { | ||
$routeProvider.when('/bar/:barId', {controller: angular.noop}); | ||
}); | ||
|
||
inject(function($route, $routeParams, $location, $rootScope) { | ||
$rootScope.$on('$routeChangeSuccess', routeChangeSpy); | ||
|
||
$location.path('/bar/1'); | ||
$rootScope.$digest(); | ||
routeChangeSpy.reset(); | ||
|
||
$route.update({barId: '2'}); | ||
$rootScope.$digest(); | ||
|
||
expect($routeParams).toEqual({barId: '2'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also assert that $location was updated correctly as well? |
||
expect(routeChangeSpy).toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
|
||
it('should support total multi-parameter route updating', function() { | ||
var routeChangeSpy = jasmine.createSpy('route change'); | ||
|
||
module(function($routeProvider) { | ||
$routeProvider.when('/bar/:barId/:fooId/:spamId/:eggId', {controller: angular.noop}); | ||
}); | ||
|
||
inject(function($route, $routeParams, $location, $rootScope) { | ||
$rootScope.$on('$routeChangeSuccess', routeChangeSpy); | ||
|
||
$location.path('/bar/1/1/1/1'); | ||
$rootScope.$digest(); | ||
routeChangeSpy.reset(); | ||
|
||
$route.update({barId: '2', fooId: '2', spamId: '2', eggId: '2'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use different values for each param? it will make the test more solid by asserting that the right values are used for the update of the right position |
||
$rootScope.$digest(); | ||
|
||
expect($routeParams).toEqual({barId: '2', fooId: '2', spamId: '2', eggId: '2'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also assert that $location was updated correctly as well? |
||
expect(routeChangeSpy).toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
|
||
it('should support partial multi-parameter route updating', function() { | ||
var routeChangeSpy = jasmine.createSpy('route change'); | ||
|
||
module(function($routeProvider) { | ||
$routeProvider.when('/bar/:barId/:fooId/:spamId/:eggId', {controller: angular.noop}); | ||
}); | ||
|
||
inject(function($route, $routeParams, $location, $rootScope) { | ||
$rootScope.$on('$routeChangeSuccess', routeChangeSpy); | ||
|
||
$location.path('/bar/1/1/1/1'); | ||
$rootScope.$digest(); | ||
routeChangeSpy.reset(); | ||
|
||
$route.update({barId: '2', fooId: '2'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use unique value for each param |
||
$rootScope.$digest(); | ||
|
||
expect($routeParams).toEqual({barId: '2', fooId: '2', spamId: '1', eggId: '1'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also assert that $location was updated correctly as well? |
||
expect(routeChangeSpy).toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('reload', function() { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe call it
updateParams
?