Skip to content

Commit 513865a

Browse files
committed
feat($state): allow disabling of transition notifications
Closes #401
1 parent a74ac92 commit 513865a

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/state.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
264264
$state.transitionTo = function transitionTo(to, toParams, options) {
265265
if (!isDefined(options)) options = (options === true || options === false) ? { location: options } : {};
266266
toParams = toParams || {};
267-
options = extend({ location: true, inherit: false, relative: null, $retry: false }, options);
267+
options = extend({ location: true, inherit: false, relative: null, notify: true, $retry: false }, options);
268268

269269
var from = $state.$current, fromParams = $state.params, fromPath = from.path;
270270

@@ -331,8 +331,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
331331
toParams = normalize(to.params, toParams || {});
332332

333333
// Broadcast start event and cancel the transition if requested
334-
evt = $rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams);
335-
if (evt.defaultPrevented) return TransitionPrevented;
334+
if (options.notify) {
335+
evt = $rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams);
336+
if (evt.defaultPrevented) return TransitionPrevented;
337+
}
336338

337339
// Resolve locals for the remaining states, but don't update any global state just
338340
// yet -- if anything fails to resolve the current state needs to remain untouched.
@@ -394,7 +396,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
394396
}
395397
}
396398

397-
$rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
399+
if (options.notify) {
400+
$rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
401+
}
398402

399403
return $state.current;
400404
}, function (error) {

test/stateSpec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,36 @@ describe('state', function () {
278278
expect($state.current).toBe(D);
279279
}));
280280

281+
it('does not trigger $stateChangeSuccess when suppressed, but changes state', inject(function ($state, $q, $rootScope) {
282+
initStateTo(E, { i: 'iii' });
283+
var called;
284+
285+
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
286+
called = true;
287+
});
288+
289+
$state.transitionTo(D, { x: '1', y: '2' }, { notify: false });
290+
$q.flush();
291+
292+
expect(called).toBeFalsy();
293+
expect($state.current).toBe(D);
294+
}));
295+
296+
it('does not trigger $stateChangeSuccess when suppressed, but updates params', inject(function ($state, $q, $rootScope) {
297+
initStateTo(E, { x: 'iii' });
298+
var called;
299+
300+
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
301+
called = true;
302+
});
303+
$state.transitionTo(E, { i: '1', y: '2' }, { notify: false });
304+
$q.flush();
305+
306+
expect(called).toBeFalsy();
307+
expect($state.params.i).toBe('1');
308+
expect($state.current).toBe(E);
309+
}));
310+
281311
it('is a no-op when passing the current state and identical parameters', inject(function ($state, $q) {
282312
initStateTo(A);
283313
var trans = $state.transitionTo(A, {}); // no-op

0 commit comments

Comments
 (0)