Skip to content

Commit

Permalink
fix(onEnter): Do not inject child-state data into ng1 onEnter hooks
Browse files Browse the repository at this point in the history
Closes #3546
  • Loading branch information
christopherthielen committed Dec 21, 2017
1 parent 01bbaf0 commit cdec6a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/statebuilders/onEnterExitRetain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import { Ng1StateDeclaration } from '../interface';
*/
export const getStateHookBuilder = (hookName: "onEnter"|"onExit"|"onRetain") =>
function stateHookBuilder(state: StateObject, parentFn: BuilderFunction): TransitionStateHookFn {
let hook = state[hookName];
let pathname = hookName === 'onExit' ? 'from' : 'to';
const hook = state[hookName];
const pathname = hookName === 'onExit' ? 'from' : 'to';

function decoratedNg1Hook(trans: Transition, state: Ng1StateDeclaration): HookResult {
let resolveContext = new ResolveContext(trans.treeChanges(pathname));
let locals = extend(getLocals(resolveContext), { $state$: state, $transition$: trans });
const resolveContext = new ResolveContext(trans.treeChanges(pathname));
const subContext = resolveContext.subContext(state.$$state());
const locals = extend(getLocals(subContext), { $state$: state, $transition$: trans });
return services.$injector.invoke(hook, this, locals);
}

Expand Down
24 changes: 24 additions & 0 deletions test/resolveSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,30 @@ describe("Integration: Resolvables system", () => {
expect(counts._J).toEqualData(1);
});

it("should not inject child data into parent", () => {
let injectedData;
router.stateRegistry.register({
name: 'foo',
resolve: {
myresolve: () => 'foodata',
},
onEnter: (myresolve) => injectedData = myresolve,
});

router.stateRegistry.register({
name: 'foo.bar',
resolve: {
myresolve: () => 'bardata',
},
});

$state.go("foo.bar");
$rootScope.$digest();

expect($state.current.name).toBe("foo.bar");
expect(injectedData).toBe('foodata');
});

it("should inject a promise for NOWAIT resolve into a controller", inject(function($compile, $rootScope) {
let scope = $rootScope.$new();
let el = $compile('<div><ui-view></ui-view></div>')(scope);
Expand Down

0 comments on commit cdec6a0

Please sign in to comment.