Skip to content

Commit

Permalink
feat($stateChangeStart): Add options to event
Browse files Browse the repository at this point in the history
- Add options as a 5th parameter to the $stateChangeStart event
- As per #1620
  • Loading branch information
scooper91 committed Aug 26, 2015
1 parent f92d3dd commit a1f0755
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* })
* </pre>
*/
if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams).defaultPrevented) {
if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams, options).defaultPrevented) {
$rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
$urlRouter.update();
return TransitionPrevented;
Expand Down
19 changes: 10 additions & 9 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ describe('state', function () {
return jasmine.getEnv().currentSpec.$injector.get(what);
}

function initStateTo(state, optionalParams) {
function initStateTo(state, optionalParams, optionalOptions) {
var $state = $get('$state'), $q = $get('$q');
$state.transitionTo(state, optionalParams || {});
$state.transitionTo(state, optionalParams || {}, optionalOptions || {});
$q.flush();
expect($state.current).toBe(state);
}
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('state', function () {
initStateTo(RS);
$location.search({term: 'hello'});
var called;
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
called = true
});
$q.flush();
Expand All @@ -223,7 +223,7 @@ describe('state', function () {
initStateTo(RS);
$location.search({term: 'hello'});
var called;
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
called = true
});
$q.flush();
Expand All @@ -235,7 +235,7 @@ describe('state', function () {
initStateTo(RS);
var called;
$state.go(".", { term: 'goodbye' });
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
called = true
});
$q.flush();
Expand All @@ -248,7 +248,7 @@ describe('state', function () {
initStateTo(RSP, { doReload: 'foo' });
expect($state.params.doReload).toEqual('foo');
var called;
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
called = true
});
$state.transitionTo(RSP, { doReload: 'bar' });
Expand All @@ -264,19 +264,20 @@ describe('state', function () {
}));

it('triggers $stateChangeStart', inject(function ($state, $q, $rootScope) {
initStateTo(E, { i: 'iii' });
initStateTo(E, { i: 'iii' }, { anOption: true });
var called;
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
expect(from).toBe(E);
expect(fromParams).toEqual({ i: 'iii' });
expect(to).toBe(D);
expect(toParams).toEqual({ x: '1', y: '2' });
expect(options.anOption).toBe(false);

expect($state.current).toBe(from); // $state not updated yet
expect($state.params).toEqual(fromParams);
called = true;
});
$state.transitionTo(D, { x: '1', y: '2' });
$state.transitionTo(D, { x: '1', y: '2' }, { anOption: false });
$q.flush();
expect(called).toBeTruthy();
expect($state.current).toBe(D);
Expand Down

0 comments on commit a1f0755

Please sign in to comment.