refactor(views): split ViewManager/ViewContainerRef.createView into 2 methods

BREAKING CHANGES:

`ViewManager.createView` / `ViewContainerRef.create` have been split into 2 methods:

- `createHostView` which takes dynamically created bindings
- `createEmbeddedView` which takes the newly introduced `TemplateRef`

The new type `TemplateRef` is the combination of a `ProtoViewRef` and and `ElementRef`
from the same place. Use `TemplateRef` when working with embedded views in
`ng-if`, `ng-for`, ... instead of `ProtoViewRef`.

Also, `ProtoViewRef` is no more injectable, but `TemplateRef` is.

First part of #1989 to clean up manual content projection.
Closes #3114
This commit is contained in:
Tobias Bosch
2015-07-17 08:03:40 -07:00
parent 762a94f2cd
commit f42382db3b
17 changed files with 198 additions and 126 deletions

View File

@ -2,7 +2,7 @@ import {Directive} from 'angular2/annotations';
import {
ViewContainerRef,
ViewRef,
ProtoViewRef,
TemplateRef,
Pipes,
LifecycleEvent,
Pipe,
@ -46,7 +46,7 @@ export class NgFor {
_ngForOf: any;
_pipe: Pipe;
constructor(private viewContainer: ViewContainerRef, private protoViewRef: ProtoViewRef,
constructor(private viewContainer: ViewContainerRef, private templateRef: TemplateRef,
private pipes: Pipes, private cdr: ChangeDetectorRef) {}
set ngForOf(value: any) {
@ -79,7 +79,7 @@ export class NgFor {
changes.forEachAddedItem((addedRecord) =>
insertTuples.push(new RecordViewTuple(addedRecord, null)));
NgFor.bulkInsert(insertTuples, this.viewContainer, this.protoViewRef);
NgFor.bulkInsert(insertTuples, this.viewContainer, this.templateRef);
for (var i = 0; i < insertTuples.length; i++) {
this._perViewChange(insertTuples[i].view, insertTuples[i].record);
@ -109,14 +109,14 @@ export class NgFor {
}
static bulkInsert(tuples: List<RecordViewTuple>, viewContainer: ViewContainerRef,
protoViewRef: ProtoViewRef): List<RecordViewTuple> {
templateRef: TemplateRef): List<RecordViewTuple> {
tuples.sort((a, b) => a.record.currentIndex - b.record.currentIndex);
for (var i = 0; i < tuples.length; i++) {
var tuple = tuples[i];
if (isPresent(tuple.view)) {
viewContainer.insert(tuple.view, tuple.record.currentIndex);
} else {
tuple.view = viewContainer.create(protoViewRef, tuple.record.currentIndex);
tuple.view = viewContainer.createEmbeddedView(templateRef, tuple.record.currentIndex);
}
}
return tuples;

View File

@ -1,5 +1,5 @@
import {Directive} from 'angular2/annotations';
import {ViewContainerRef, ProtoViewRef} from 'angular2/core';
import {ViewContainerRef, TemplateRef} from 'angular2/core';
import {isBlank} from 'angular2/src/facade/lang';
/**
@ -27,19 +27,19 @@ import {isBlank} from 'angular2/src/facade/lang';
@Directive({selector: '[ng-if]', properties: ['ngIf']})
export class NgIf {
viewContainer: ViewContainerRef;
protoViewRef: ProtoViewRef;
templateRef: TemplateRef;
prevCondition: boolean;
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef) {
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef) {
this.viewContainer = viewContainer;
this.prevCondition = null;
this.protoViewRef = protoViewRef;
this.templateRef = templateRef;
}
set ngIf(newCondition /* boolean */) {
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
this.prevCondition = true;
this.viewContainer.create(this.protoViewRef);
this.viewContainer.createEmbeddedView(this.templateRef);
} else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) {
this.prevCondition = false;
this.viewContainer.clear();

View File

@ -1,19 +1,19 @@
import {Directive} from 'angular2/annotations';
import {Parent} from 'angular2/di';
import {ViewContainerRef, ProtoViewRef} from 'angular2/core';
import {ViewContainerRef, TemplateRef} from 'angular2/core';
import {isPresent, isBlank, normalizeBlank} from 'angular2/src/facade/lang';
import {ListWrapper, List, MapWrapper, Map} from 'angular2/src/facade/collection';
export class SwitchView {
_viewContainerRef: ViewContainerRef;
_protoViewRef: ProtoViewRef;
_templateRef: TemplateRef;
constructor(viewContainerRef: ViewContainerRef, protoViewRef: ProtoViewRef) {
this._protoViewRef = protoViewRef;
constructor(viewContainerRef: ViewContainerRef, templateRef: TemplateRef) {
this._templateRef = templateRef;
this._viewContainerRef = viewContainerRef;
}
create() { this._viewContainerRef.create(this._protoViewRef); }
create() { this._viewContainerRef.createEmbeddedView(this._templateRef); }
destroy() { this._viewContainerRef.clear(); }
}
@ -156,12 +156,12 @@ export class NgSwitchWhen {
_switch: NgSwitch;
_view: SwitchView;
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef,
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
@Parent() sswitch: NgSwitch) {
// `_whenDefault` is used as a marker for a not yet initialized value
this._value = _whenDefault;
this._switch = sswitch;
this._view = new SwitchView(viewContainer, protoViewRef);
this._view = new SwitchView(viewContainer, templateRef);
}
onDestroy() { this._switch; }
@ -186,9 +186,9 @@ export class NgSwitchWhen {
*/
@Directive({selector: '[ng-switch-default]'})
export class NgSwitchDefault {
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef,
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
@Parent() sswitch: NgSwitch) {
sswitch._registerView(_whenDefault, new SwitchView(viewContainer, protoViewRef));
sswitch._registerView(_whenDefault, new SwitchView(viewContainer, templateRef));
}
}