From a1f07559ec74e10ff80bc4be81f287e3772b8fcb Mon Sep 17 00:00:00 2001 From: Sophie Cooper Date: Wed, 26 Aug 2015 15:41:37 +0100 Subject: [PATCH] feat($stateChangeStart): Add options to event - Add options as a 5th parameter to the $stateChangeStart event - As per #1620 --- src/state.js | 2 +- test/stateSpec.js | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/state.js b/src/state.js index 1df961b94..a61fa5f38 100644 --- a/src/state.js +++ b/src/state.js @@ -1077,7 +1077,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { * }) * */ - 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; diff --git a/test/stateSpec.js b/test/stateSpec.js index 9273f95fd..a8f45ac4a 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -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); } @@ -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(); @@ -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(); @@ -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(); @@ -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' }); @@ -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);