Skip to content

Commit 8a24041

Browse files
refactor(ng2.uiView): make injecte parent context into a single object
fix(ng2.uiView): do not dispose ref twice. do not deregister before onInit
1 parent ec1c534 commit 8a24041

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/ng2/providers.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ export const UIROUTER_PROVIDERS: Provider[] = [
110110

111111
provide(UIRouterGlobals, { useFactory: (r: UIRouter) => { return r.globals; }, deps: [UIRouter]}),
112112

113-
provide(UiView.INJECT.context, { useFactory: (r: StateRegistry) => { return r.root(); }, deps: [StateRegistry]} ),
114-
115-
provide(UiView.INJECT.fqn, { useValue: null })
113+
provide(UiView.PARENT_INJECT, { useFactory: (r: StateRegistry) => { return { fqn: null, context: r.root() } }, deps: [StateRegistry]} )
116114

117115
];
118116

src/ng2/uiSref.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {Directive, Inject, Input} from "angular2/core";
44
import {Optional} from "angular2/core";
55
import {ElementRef} from "angular2/core";
66
import {Renderer} from "angular2/core";
7-
import {UiView} from "./uiView";
8-
import {ViewContext} from "../view/interface";
7+
import {UiView, ParentUiViewInject} from "./uiView";
98
import {extend} from "../common/common";
109

1110
/** @hidden */
@@ -69,7 +68,7 @@ export class UiSref {
6968

7069
constructor(
7170
private _router: UIRouter,
72-
@Inject(UiView.INJECT.context) public context: ViewContext,
71+
@Inject(UiView.PARENT_INJECT) public parent: ParentUiViewInject,
7372
@Optional() private _anchorUiSref: AnchorUiSref
7473
) { }
7574

@@ -88,7 +87,7 @@ export class UiSref {
8887
}
8988

9089
getOptions() {
91-
let defOpts = { relative: this.context.name, inherit: true };
90+
let defOpts = { relative: this.parent && this.parent.context && this.parent.context.name, inherit: true };
9291
return extend(defOpts, this.options || {});
9392
}
9493

src/ng2/uiView.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const getProviders = (injector) => {
2323
return providers;
2424
};
2525

26+
// These are provide()d as the string UiView.PARENT_INJECT
27+
export interface ParentUiViewInject {
28+
context: ViewContext;
29+
fqn: string;
30+
}
31+
2632
/**
2733
* A UI-Router viewport directive, which is filled in by a view (component) on a state.
2834
*
@@ -99,29 +105,25 @@ export class UiView {
99105
deregister: Function;
100106
uiViewData: any = {};
101107

102-
static INJECT = {
103-
fqn: "UiView.parentFQN",
104-
context: "UiView.parentContext"
105-
};
108+
static PARENT_INJECT = "UiView.PARENT_INJECT";
106109

107110
constructor(
108111
public router: UIRouter,
109-
@Inject(UiView.INJECT.context) public parentContext: ViewContext,
110-
@Inject(UiView.INJECT.fqn) public parentFqn: string,
112+
@Inject(UiView.PARENT_INJECT) public parent: ParentUiViewInject,
111113
public dcl: DynamicComponentLoader,
112114
public elementRef: ElementRef,
113115
public injector: Injector
114116
) { }
115117

116118
ngOnInit() {
117-
let parentFqn = this.parentFqn;
119+
let parentFqn = this.parent.fqn;
118120
let name = this.name || '$default';
119121

120122
this.uiViewData = {
121123
id: id++,
122124
name: name,
123125
fqn: parentFqn ? parentFqn + "." + name : name,
124-
creationContext: this.parentContext,
126+
creationContext: this.parent.context,
125127
configUpdated: this.viewConfigUpdated.bind(this),
126128
config: undefined
127129
};
@@ -131,10 +133,11 @@ export class UiView {
131133

132134
disposeLast() {
133135
if (this.componentRef) this.componentRef.dispose();
136+
this.componentRef = null;
134137
}
135138

136139
ngOnDestroy() {
137-
this.deregister();
140+
if (this.deregister) this.deregister();
138141
this.disposeLast();
139142
}
140143

@@ -159,11 +162,10 @@ export class UiView {
159162
let rc = config.node.resolveContext;
160163
let resolvables = rc.getResolvables();
161164
let rawProviders = Object.keys(resolvables).map(key => provide(key, { useValue: resolvables[key].data }));
162-
rawProviders.push(provide(UiView.INJECT.context, { useValue: config.viewDecl.$context }));
163-
rawProviders.push(provide(UiView.INJECT.fqn, { useValue: uiViewData.fqn }));
165+
rawProviders.push(provide(UiView.PARENT_INJECT, { useValue: { context: config.viewDecl.$context, fqn: uiViewData.fqn } }));
164166
let providers = Injector.resolve(rawProviders);
165167

166-
let exclusions = [UiView.INJECT.context, UiView.INJECT.fqn];
168+
let exclusions = [UiView.PARENT_INJECT];
167169
providers = getProviders(injector).filter(x => exclusions.indexOf(x.key.displayName) === -1).concat(providers);
168170

169171
// The 'controller' should be a Component class

0 commit comments

Comments
 (0)