Skip to content

Commit ee5df00

Browse files
committed
refactor(directives): minor cleanup & refactoring
Closes angular#3629
1 parent 89a0f24 commit ee5df00

File tree

2 files changed

+28
-53
lines changed

2 files changed

+28
-53
lines changed

modules/angular2/src/directives/ng_if.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,17 @@ import {isBlank} from 'angular2/src/facade/lang';
2626
*/
2727
@Directive({selector: '[ng-if]', properties: ['ngIf']})
2828
export class NgIf {
29-
viewContainer: ViewContainerRef;
30-
templateRef: TemplateRef;
31-
prevCondition: boolean;
29+
private _prevCondition: boolean = null;
3230

33-
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef) {
34-
this.viewContainer = viewContainer;
35-
this.prevCondition = null;
36-
this.templateRef = templateRef;
37-
}
31+
constructor(private _viewContainer: ViewContainerRef, private _templateRef: TemplateRef) {}
3832

3933
set ngIf(newCondition /* boolean */) {
40-
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
41-
this.prevCondition = true;
42-
this.viewContainer.createEmbeddedView(this.templateRef);
43-
} else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) {
44-
this.prevCondition = false;
45-
this.viewContainer.clear();
34+
if (newCondition && (isBlank(this._prevCondition) || !this._prevCondition)) {
35+
this._prevCondition = true;
36+
this._viewContainer.createEmbeddedView(this._templateRef);
37+
} else if (!newCondition && (isBlank(this._prevCondition) || this._prevCondition)) {
38+
this._prevCondition = false;
39+
this._viewContainer.clear();
4640
}
4741
}
4842
}

modules/angular2/src/directives/ng_switch.ts

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import {Directive} from 'angular2/annotations';
22
import {Host} from 'angular2/di';
33
import {ViewContainerRef, TemplateRef} from 'angular2/core';
4-
import {isPresent, isBlank, normalizeBlank} from 'angular2/src/facade/lang';
5-
import {ListWrapper, List, MapWrapper, Map} from 'angular2/src/facade/collection';
4+
import {isPresent, isBlank, normalizeBlank, CONST_EXPR} from 'angular2/src/facade/lang';
5+
import {ListWrapper, List, Map} from 'angular2/src/facade/collection';
66

7-
export class SwitchView {
8-
_viewContainerRef: ViewContainerRef;
9-
_templateRef: TemplateRef;
7+
const _WHEN_DEFAULT = CONST_EXPR(new Object());
108

11-
constructor(viewContainerRef: ViewContainerRef, templateRef: TemplateRef) {
12-
this._templateRef = templateRef;
13-
this._viewContainerRef = viewContainerRef;
14-
}
9+
export class SwitchView {
10+
constructor(private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef) {}
1511

16-
create() { this._viewContainerRef.createEmbeddedView(this._templateRef); }
12+
create(): void { this._viewContainerRef.createEmbeddedView(this._templateRef); }
1713

18-
destroy() { this._viewContainerRef.clear(); }
14+
destroy(): void { this._viewContainerRef.clear(); }
1915
}
2016

2117
/**
@@ -45,16 +41,10 @@ export class SwitchView {
4541
*/
4642
@Directive({selector: '[ng-switch]', properties: ['ngSwitch']})
4743
export class NgSwitch {
48-
_switchValue: any;
49-
_useDefault: boolean;
50-
_valueViews: Map<any, List<SwitchView>>;
51-
_activeViews: List<SwitchView>;
52-
53-
constructor() {
54-
this._valueViews = new Map();
55-
this._activeViews = [];
56-
this._useDefault = false;
57-
}
44+
private _switchValue: any;
45+
private _useDefault: boolean = false;
46+
private _valueViews: Map<any, List<SwitchView>> = new Map();
47+
private _activeViews: List<SwitchView> = [];
5848

5949
set ngSwitch(value) {
6050
// Empty the currently active ViewContainers
@@ -65,7 +55,7 @@ export class NgSwitch {
6555
var views = this._valueViews.get(value);
6656
if (isBlank(views)) {
6757
this._useDefault = true;
68-
views = normalizeBlank(this._valueViews.get(_whenDefault));
58+
views = normalizeBlank(this._valueViews.get(_WHEN_DEFAULT));
6959
}
7060
this._activateViews(views);
7161

@@ -91,7 +81,7 @@ export class NgSwitch {
9181
// Switch to default when there is no more active ViewContainers
9282
if (this._activeViews.length === 0 && !this._useDefault) {
9383
this._useDefault = true;
94-
this._activateViews(this._valueViews.get(_whenDefault));
84+
this._activateViews(this._valueViews.get(_WHEN_DEFAULT));
9585
}
9686
}
9787

@@ -123,18 +113,17 @@ export class NgSwitch {
123113
}
124114

125115
_deregisterView(value, view: SwitchView): void {
126-
// `_whenDefault` is used a marker for non-registered whens
127-
if (value == _whenDefault) return;
116+
// `_WHEN_DEFAULT` is used a marker for non-registered whens
117+
if (value === _WHEN_DEFAULT) return;
128118
var views = this._valueViews.get(value);
129119
if (views.length == 1) {
130-
MapWrapper.delete(this._valueViews, value);
120+
this._valueViews.delete(value);
131121
} else {
132122
ListWrapper.remove(views, view);
133123
}
134124
}
135125
}
136126

137-
138127
/**
139128
* Defines a case statement as an expression.
140129
*
@@ -152,27 +141,21 @@ export class NgSwitch {
152141
*/
153142
@Directive({selector: '[ng-switch-when]', properties: ['ngSwitchWhen']})
154143
export class NgSwitchWhen {
155-
_value: any;
156-
_switch: NgSwitch;
144+
// `_WHEN_DEFAULT` is used as a marker for a not yet initialized value
145+
_value: any = _WHEN_DEFAULT;
157146
_view: SwitchView;
158147

159148
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
160-
@Host() sswitch: NgSwitch) {
161-
// `_whenDefault` is used as a marker for a not yet initialized value
162-
this._value = _whenDefault;
163-
this._switch = sswitch;
149+
@Host() private _switch: NgSwitch) {
164150
this._view = new SwitchView(viewContainer, templateRef);
165151
}
166152

167-
onDestroy() { this._switch; }
168-
169153
set ngSwitchWhen(value) {
170154
this._switch._onWhenValueChanged(this._value, value, this._view);
171155
this._value = value;
172156
}
173157
}
174158

175-
176159
/**
177160
* Defines a default case statement.
178161
*
@@ -188,8 +171,6 @@ export class NgSwitchWhen {
188171
export class NgSwitchDefault {
189172
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
190173
@Host() sswitch: NgSwitch) {
191-
sswitch._registerView(_whenDefault, new SwitchView(viewContainer, templateRef));
174+
sswitch._registerView(_WHEN_DEFAULT, new SwitchView(viewContainer, templateRef));
192175
}
193176
}
194-
195-
var _whenDefault = new Object();

0 commit comments

Comments
 (0)