Skip to content

add flag in .transitionTo() to allow ability to disable broadcast of $stateChangeSuccess #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
8 changes: 6 additions & 2 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
$state.transitionTo = function transitionTo(to, toParams, options) {
if (!isDefined(options)) options = (options === true || options === false) ? { location: options } : {};
toParams = toParams || {};
options = extend({ location: true, inherit: false, relative: null }, options);
options = extend({ location: true, inherit: false, relative: null, broadcastStateChangeSuccess : true }, options);

var toState = findState(to, options.relative);

Expand Down Expand Up @@ -274,6 +274,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
var transition = $state.transition = resolved.then(function () {
var l, entering, exiting;


if ($state.transition !== transition) return TransitionSuperseded;

// Exit 'from' states not kept
Expand Down Expand Up @@ -307,7 +308,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
$location.url(toNav.url.format(toNav.locals.globals.$stateParams));
}

$rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
if(options.broadcastStateChangeSuccess){
$rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
}


return $state.current;
}, function (error) {
Expand Down
26 changes: 26 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,32 @@ describe('state', function () {
expect($state.current).toBe(D);
}));


it('does not trigger $stateChangeSuccess when flag prevents, but still transitions when different state', inject(function ($state, $q, $rootScope) {
initStateTo(E, { i: 'iii' });
var called;
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
called = true;
});
$state.transitionTo(D, { x: '1', y: '2' }, {broadcastStateChangeSuccess : false});
$q.flush();
expect(called).toBeFalsy();
expect($state.current).toBe(D);
}));

it('does not trigger $stateChangeSuccess when flag prevents, yet still updates params', inject(function ($state, $q, $rootScope) {
initStateTo(E, { x: 'iii' });
var called;
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
called = true;
});
$state.transitionTo(E, { i: '1', y: '2' }, {broadcastStateChangeSuccess : false});
$q.flush();
expect(called).toBeFalsy();
expect($state.params.i).toBe('1');
expect($state.current).toBe(E);
}));

it('is a no-op when passing the current state and identical parameters', inject(function ($state, $q) {
initStateTo(A);
var trans = $state.transitionTo(A, {}); // no-op
Expand Down