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

feat(ngRoute): add method for changing url params #8358

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call it updateParams?

newParams = defaults(newParams, this.current.params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the defaults helper, use extend({}, this.current.params, newParams)

$location.path(interpolate(this.current.$$route.originalPath, newParams));
}
};

Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}];
}
67 changes: 67 additions & 0 deletions test/ngRoute/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Copy link
Contributor

Choose a reason for hiding this comment

The 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'});
Copy link
Contributor

Choose a reason for hiding this comment

The 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'});
Copy link
Contributor

Choose a reason for hiding this comment

The 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'});
Copy link
Contributor

Choose a reason for hiding this comment

The 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'});
Copy link
Contributor

Choose a reason for hiding this comment

The 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() {

Expand Down