Permalink
Browse files

fix(onExit): inject resolve values using the "from path"

fix(onExit): inject the current transition as `$transition$`

Closes #3081
1 parent 9d2661c commit c91b65a8a9c0f684d3d154601d52ce7e6cbc1619 @christopherthielen christopherthielen committed Nov 2, 2016
Showing with 34 additions and 4 deletions.
  1. +5 −2 src/ng1/statebuilders/onEnterExitRetain.ts
  2. +29 −2 test/stateSpec.js
@@ -14,9 +14,12 @@ import { getLocals } from "../services";
export const getStateHookBuilder = (hookName: "onEnter"|"onExit"|"onRetain") =>
function stateHookBuilder(state: State, parentFn: BuilderFunction): TransitionStateHookFn {
let hook = state[hookName];
+ let pathname = hookName === 'onExit' ? 'from' : 'to';
+
function decoratedNg1Hook(trans: Transition, state: State): HookResult {
- let resolveContext = new ResolveContext(trans.treeChanges().to);
- return services.$injector.invoke(hook, this, extend({ $state$: state }, getLocals(resolveContext)));
+ let resolveContext = new ResolveContext(trans.treeChanges(pathname));
+ var locals = extend(getLocals(resolveContext), { $state$: state, $transition$: trans });
+ return services.$injector.invoke(hook, this, locals);
}
return hook ? decoratedNg1Hook : undefined;
View
@@ -11,12 +11,13 @@ var html5Compat = require('./util/testUtilsNg1').html5Compat;
describe('state', function () {
- var $injector, $stateProvider, locationProvider, templateParams, template, ctrlName, errors;
+ var $uiRouter, $injector, $stateProvider, locationProvider, templateParams, template, ctrlName, errors;
- beforeEach(module('ui.router', function($locationProvider) {
+ beforeEach(module('ui.router', function($locationProvider, $uiRouterProvider) {
errors = [];
locationProvider = $locationProvider;
$locationProvider.html5Mode(false);
+ $uiRouter = $uiRouterProvider.router;
}));
var log, logEvents, logEnterExit;
@@ -635,6 +636,32 @@ describe('state', function () {
'A.onEnter;');
}));
+ // test for #3081
+ it('injects resolve values from the exited state into onExit', (function(done) {
+ const registry = $uiRouter.stateRegistry;
+ registry.register({
+ name: 'design',
+ url: '/design',
+ resolve: { cc: function() { return 'cc resolve'; } },
+ onExit: function(cc, $state$, $transition$) {
+ expect($transition$.to().name).toBe('A');
+ expect($transition$.from().name).toBe('design');
+
+ expect($state$.self).toBe(registry.get('design'));
+
+ expect(cc).toBe('cc resolve');
+
+ done();
+ }
+ });
+
+ $state.go("design");
+ $q.flush();
+
+ $state.go("A");
+ $q.flush();
+ }));
+
it('doesn\'t transition to parent state when child has no URL', inject(function ($state, $q) {
$state.transitionTo('about.sidebar'); $q.flush();
expect($state.current.name).toEqual('about.sidebar');

0 comments on commit c91b65a

Please sign in to comment.