Skip to content

Commit ce6a2ba

Browse files
committed
refactor(view): moved the logic from ProtoView to ProtoViewFactory
1 parent 0f4a089 commit ce6a2ba

File tree

16 files changed

+305
-286
lines changed

16 files changed

+305
-286
lines changed

modules/angular2/src/change_detection/change_detection.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import {IterableChangesFactory} from './pipes/iterable_changes';
44
import {KeyValueChangesFactory} from './pipes/keyvalue_changes';
55
import {AsyncPipeFactory} from './pipes/async_pipe';
66
import {NullPipeFactory} from './pipes/null_pipe';
7+
import {BindingRecord} from './binding_record';
8+
import {DirectiveRecord} from './directive_record';
79
import {DEFAULT} from './constants';
810
import {ChangeDetection, ProtoChangeDetector} from './interfaces';
911
import {Injectable} from 'angular2/di';
12+
import {List} from 'angular2/src/facade/collection';
13+
1014

1115
/**
1216
* Structural diffing for `Object`s and `Map`s.
@@ -61,8 +65,9 @@ export class DynamicChangeDetection extends ChangeDetection {
6165
this.registry = registry;
6266
}
6367

64-
createProtoChangeDetector(name:string, changeControlStrategy:string = DEFAULT):ProtoChangeDetector{
65-
return new DynamicProtoChangeDetector(this.registry, changeControlStrategy);
68+
createProtoChangeDetector(name:string, bindingRecords:List<BindingRecord>, variableBindings:List<string>,
69+
directiveRecords:List<DirectiveRecord>, changeControlStrategy:string = DEFAULT):ProtoChangeDetector{
70+
return new DynamicProtoChangeDetector(this.registry, bindingRecords, variableBindings, directiveRecords, changeControlStrategy);
6671
}
6772
}
6873

@@ -82,8 +87,9 @@ export class JitChangeDetection extends ChangeDetection {
8287
this.registry = registry;
8388
}
8489

85-
createProtoChangeDetector(name:string, changeControlStrategy:string = DEFAULT):ProtoChangeDetector{
86-
return new JitProtoChangeDetector(this.registry, changeControlStrategy);
90+
createProtoChangeDetector(name:string, bindingRecords:List<BindingRecord>, variableBindings:List<string>,
91+
directiveRecords:List<DirectiveRecord>, changeControlStrategy:string = DEFAULT):ProtoChangeDetector{
92+
return new JitProtoChangeDetector(this.registry, bindingRecords, variableBindings, directiveRecords, changeControlStrategy);
8793
}
8894
}
8995

modules/angular2/src/change_detection/interfaces.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {DEFAULT} from './constants';
44
import {BindingRecord} from './binding_record';
55

66
export class ProtoChangeDetector {
7-
instantiate(dispatcher:any, bindingRecords:List, variableBindings:List, directiveRecords:List):ChangeDetector{
7+
instantiate(dispatcher:any):ChangeDetector{
88
return null;
99
}
1010
}
@@ -33,7 +33,8 @@ export class ProtoChangeDetector {
3333
* @exportedAs angular2/change_detection
3434
*/
3535
export class ChangeDetection {
36-
createProtoChangeDetector(name:string, changeControlStrategy:string=DEFAULT):ProtoChangeDetector{
36+
createProtoChangeDetector(name:string, bindingRecords:List, variableBindings:List, directiveRecords:List,
37+
changeControlStrategy:string=DEFAULT):ProtoChangeDetector{
3738
return null;
3839
}
3940
}

modules/angular2/src/change_detection/proto_change_detector.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {DynamicChangeDetector} from './dynamic_change_detector';
2828
import {ChangeDetectorJITGenerator} from './change_detection_jit_generator';
2929
import {PipeRegistry} from './pipes/pipe_registry';
3030
import {BindingRecord} from './binding_record';
31-
import {DirectiveIndex} from './directive_record';
31+
import {DirectiveRecord, DirectiveIndex} from './directive_record';
3232

3333
import {coalesce} from './coalesce';
3434

@@ -50,25 +50,31 @@ import {
5050
export class DynamicProtoChangeDetector extends ProtoChangeDetector {
5151
_pipeRegistry:PipeRegistry;
5252
_records:List<ProtoRecord>;
53+
_bindingRecords:List<BindingRecord>;
54+
_variableBindings:List<string>;
55+
_directiveRecords:List<DirectiveRecord>;
5356
_changeControlStrategy:string;
5457

55-
constructor(pipeRegistry:PipeRegistry, changeControlStrategy:string) {
58+
constructor(pipeRegistry:PipeRegistry, bindingRecords:List, variableBindings:List, directiveRecords:List, changeControlStrategy:string) {
5659
super();
5760
this._pipeRegistry = pipeRegistry;
61+
this._bindingRecords = bindingRecords;
62+
this._variableBindings = variableBindings;
63+
this._directiveRecords = directiveRecords;
5864
this._changeControlStrategy = changeControlStrategy;
5965
}
6066

61-
instantiate(dispatcher:any, bindingRecords:List, variableBindings:List, directiveRecords:List) {
62-
this._createRecordsIfNecessary(bindingRecords, variableBindings);
67+
instantiate(dispatcher:any) {
68+
this._createRecordsIfNecessary();
6369
return new DynamicChangeDetector(this._changeControlStrategy, dispatcher,
64-
this._pipeRegistry, this._records, directiveRecords);
70+
this._pipeRegistry, this._records, this._directiveRecords);
6571
}
6672

67-
_createRecordsIfNecessary(bindingRecords:List, variableBindings:List) {
73+
_createRecordsIfNecessary() {
6874
if (isBlank(this._records)) {
6975
var recordBuilder = new ProtoRecordBuilder();
70-
ListWrapper.forEach(bindingRecords, (b) => {
71-
recordBuilder.addAst(b, variableBindings);
76+
ListWrapper.forEach(this._bindingRecords, (b) => {
77+
recordBuilder.addAst(b, this._variableBindings);
7278
});
7379
this._records = coalesce(recordBuilder.records);
7480
}
@@ -79,31 +85,37 @@ var _jitProtoChangeDetectorClassCounter:number = 0;
7985
export class JitProtoChangeDetector extends ProtoChangeDetector {
8086
_factory:Function;
8187
_pipeRegistry;
88+
_bindingRecords:List<BindingRecord>;
89+
_variableBindings:List<string>;
90+
_directiveRecords:List<DirectiveRecord>;
8291
_changeControlStrategy:string;
8392

84-
constructor(pipeRegistry, changeControlStrategy:string) {
93+
constructor(pipeRegistry, bindingRecords:List, variableBindings:List, directiveRecords:List, changeControlStrategy:string) {
8594
super();
8695
this._pipeRegistry = pipeRegistry;
8796
this._factory = null;
97+
this._bindingRecords = bindingRecords;
98+
this._variableBindings = variableBindings;
99+
this._directiveRecords = directiveRecords;
88100
this._changeControlStrategy = changeControlStrategy;
89101
}
90102

91-
instantiate(dispatcher:any, bindingRecords:List, variableBindings:List, directiveRecords:List) {
92-
this._createFactoryIfNecessary(bindingRecords, variableBindings, directiveRecords);
103+
instantiate(dispatcher:any) {
104+
this._createFactoryIfNecessary();
93105
return this._factory(dispatcher, this._pipeRegistry);
94106
}
95107

96-
_createFactoryIfNecessary(bindingRecords:List, variableBindings:List, directiveRecords:List) {
108+
_createFactoryIfNecessary() {
97109
if (isBlank(this._factory)) {
98110
var recordBuilder = new ProtoRecordBuilder();
99-
ListWrapper.forEach(bindingRecords, (b) => {
100-
recordBuilder.addAst(b, variableBindings);
111+
ListWrapper.forEach(this._bindingRecords, (b) => {
112+
recordBuilder.addAst(b, this._variableBindings);
101113
});
102114
var c = _jitProtoChangeDetectorClassCounter++;
103115
var records = coalesce(recordBuilder.records);
104116
var typeName = `ChangeDetector${c}`;
105117
this._factory = new ChangeDetectorJITGenerator(typeName, this._changeControlStrategy, records,
106-
directiveRecords).generate();
118+
this._directiveRecords).generate();
107119
}
108120
}
109121
}

modules/angular2/src/core/compiler/compiler.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class Compiler {
9494

9595
var directiveMetadata = Compiler.buildRenderDirective(componentBinding);
9696
return this._renderer.createHostProtoView(directiveMetadata).then( (hostRenderPv) => {
97-
return this._compileNestedProtoViews(null, hostRenderPv, [componentBinding], true);
97+
return this._compileNestedProtoViews(null, null, hostRenderPv, [componentBinding], true);
9898
}).then( (appProtoView) => {
9999
return new ProtoViewRef(appProtoView);
100100
});
@@ -135,7 +135,7 @@ export class Compiler {
135135
if (isPresent(template.renderer)) {
136136
var directives = [];
137137
pvPromise = this._renderer.createImperativeComponentProtoView(template.renderer).then( (renderPv) => {
138-
return this._compileNestedProtoViews(componentBinding, renderPv, directives, true);
138+
return this._compileNestedProtoViews(null, componentBinding, renderPv, directives, true);
139139
});
140140
} else {
141141
var directives = ListWrapper.map(
@@ -144,7 +144,7 @@ export class Compiler {
144144
);
145145
var renderTemplate = this._buildRenderTemplate(component, template, directives);
146146
pvPromise = this._renderer.compile(renderTemplate).then( (renderPv) => {
147-
return this._compileNestedProtoViews(componentBinding, renderPv, directives, true);
147+
return this._compileNestedProtoViews(null, componentBinding, renderPv, directives, true);
148148
});
149149
}
150150

@@ -153,9 +153,9 @@ export class Compiler {
153153
}
154154

155155
// TODO(tbosch): union type return AppProtoView or Promise<AppProtoView>
156-
_compileNestedProtoViews(componentBinding, renderPv, directives, isComponentRootView) {
156+
_compileNestedProtoViews(parentProtoView, componentBinding, renderPv, directives, isComponentRootView) {
157157
var nestedPVPromises = [];
158-
var protoView = this._protoViewFactory.createProtoView(componentBinding, renderPv, directives);
158+
var protoView = this._protoViewFactory.createProtoView(parentProtoView, componentBinding, renderPv, directives);
159159
if (isComponentRootView && isPresent(componentBinding)) {
160160
// Populate the cache before compiling the nested components,
161161
// so that components can reference themselves in their template.
@@ -170,15 +170,12 @@ export class Compiler {
170170
var nestedRenderProtoView = renderPv.elementBinders[binderIndex].nestedProtoView;
171171
var elementBinderDone = (nestedPv) => {
172172
elementBinder.nestedProtoView = nestedPv;
173-
// Can't set the parentProtoView for components,
174-
// as their AppProtoView might be used in multiple other components.
175-
nestedPv.parentProtoView = isPresent(nestedComponent) ? null : protoView;
176173
};
177174
var nestedCall = null;
178175
if (isPresent(nestedComponent)) {
179176
nestedCall = this._compile(nestedComponent);
180177
} else if (isPresent(nestedRenderProtoView)) {
181-
nestedCall = this._compileNestedProtoViews(componentBinding, nestedRenderProtoView, directives, false);
178+
nestedCall = this._compileNestedProtoViews(protoView, componentBinding, nestedRenderProtoView, directives, false);
182179
}
183180
if (PromiseWrapper.isPromise(nestedCall)) {
184181
ListWrapper.push(nestedPVPromises, nestedCall.then(elementBinderDone));

0 commit comments

Comments
 (0)