X Tutup
Skip to content

Commit a9ce454

Browse files
committed
fix(change_detection): fixed reflect properties as attributes
Closes #3761
1 parent b614639 commit a9ce454

File tree

28 files changed

+168
-142
lines changed

28 files changed

+168
-142
lines changed

modules/angular2/change_detection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export {
2828
KeyValueDiffers,
2929
KeyValueDiffer,
3030
KeyValueDifferFactory
31+
3132
} from 'angular2/src/change_detection/change_detection';

modules/angular2/render.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ export {
1717
ViewDefinition,
1818
DOCUMENT,
1919
APP_ID,
20-
DOM_REFLECT_PROPERTIES_AS_ATTRIBUTES,
2120
MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE
2221
} from './src/render/render';

modules/angular2/src/change_detection/abstract_change_detector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
211211
this.dispatcher.notifyOnBinding(this._currentBinding(), value);
212212
}
213213

214+
protected logBindingUpdate(value: any): void {
215+
this.dispatcher.logBindingUpdate(this._currentBinding(), value);
216+
}
217+
214218
protected addChange(changes: StringMap<string, any>, oldValue: any,
215219
newValue: any): StringMap<string, any> {
216220
if (isBlank(changes)) {

modules/angular2/src/change_detection/binding_record.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export class BindingRecord {
6464

6565
static createForDirective(ast: AST, propertyName: string, setter: SetterFn,
6666
directiveRecord: DirectiveRecord): BindingRecord {
67-
var t = new BindingTarget(DIRECTIVE, null, propertyName, null, ast.toString());
67+
var elementIndex = directiveRecord.directiveIndex.elementIndex;
68+
var t = new BindingTarget(DIRECTIVE, elementIndex, propertyName, null, ast.toString());
6869
return new BindingRecord(DIRECTIVE, t, 0, ast, setter, null, directiveRecord);
6970
}
7071

modules/angular2/src/change_detection/change_detection.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,26 @@ export const defaultKeyValueDiffers = CONST_EXPR(new KeyValueDiffers(keyValDiff)
8484
// dart2js. See https://github.com/dart-lang/sdk/issues/23630 for details.
8585
export var preGeneratedProtoDetectors: StringMap<string, Function> = {};
8686

87-
export const PROTO_CHANGE_DETECTOR = CONST_EXPR(new OpaqueToken('ProtoChangeDetectors'));
88-
8987
/**
9088
* Implements change detection using a map of pregenerated proto detectors.
9189
*/
9290
@Injectable()
9391
export class PreGeneratedChangeDetection extends ChangeDetection {
9492
_dynamicChangeDetection: ChangeDetection;
9593
_protoChangeDetectorFactories: StringMap<string, Function>;
94+
_genConfig: ChangeDetectorGenConfig;
9695

97-
constructor(@Inject(PROTO_CHANGE_DETECTOR) @Optional() protoChangeDetectorsForTest?:
98-
StringMap<string, Function>) {
96+
constructor(config?: ChangeDetectorGenConfig,
97+
protoChangeDetectorsForTest?: StringMap<string, Function>) {
9998
super();
10099
this._dynamicChangeDetection = new DynamicChangeDetection();
101100
this._protoChangeDetectorFactories = isPresent(protoChangeDetectorsForTest) ?
102101
protoChangeDetectorsForTest :
103102
preGeneratedProtoDetectors;
103+
104+
this._genConfig =
105+
isPresent(config) ? config : new ChangeDetectorGenConfig(assertionsEnabled(),
106+
assertionsEnabled(), false);
104107
}
105108

106109
static isSupported(): boolean { return PregenProtoChangeDetector.isSupported(); }
@@ -112,11 +115,8 @@ export class PreGeneratedChangeDetection extends ChangeDetection {
112115
return this._dynamicChangeDetection.getProtoChangeDetector(id, definition);
113116
}
114117

118+
get genConfig(): ChangeDetectorGenConfig { return this._genConfig; }
115119
get generateDetectors(): boolean { return true; }
116-
117-
get genConfig(): ChangeDetectorGenConfig {
118-
return new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
119-
}
120120
}
121121

122122

@@ -127,15 +127,21 @@ export class PreGeneratedChangeDetection extends ChangeDetection {
127127
*/
128128
@Injectable()
129129
export class DynamicChangeDetection extends ChangeDetection {
130+
_genConfig: ChangeDetectorGenConfig;
131+
132+
constructor(config?: ChangeDetectorGenConfig) {
133+
super();
134+
this._genConfig =
135+
isPresent(config) ? config : new ChangeDetectorGenConfig(assertionsEnabled(),
136+
assertionsEnabled(), false);
137+
}
138+
130139
getProtoChangeDetector(id: string, definition: ChangeDetectorDefinition): ProtoChangeDetector {
131140
return new DynamicProtoChangeDetector(definition);
132141
}
133142

143+
get genConfig(): ChangeDetectorGenConfig { return this._genConfig; }
134144
get generateDetectors(): boolean { return true; }
135-
136-
get genConfig(): ChangeDetectorGenConfig {
137-
return new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
138-
}
139145
}
140146

141147
/**
@@ -145,17 +151,21 @@ export class DynamicChangeDetection extends ChangeDetection {
145151
* {@link DynamicChangeDetection} and {@link PreGeneratedChangeDetection}.
146152
*/
147153
@Injectable()
148-
@CONST()
149154
export class JitChangeDetection extends ChangeDetection {
155+
_genConfig: ChangeDetectorGenConfig;
156+
constructor(config?: ChangeDetectorGenConfig) {
157+
super();
158+
this._genConfig =
159+
isPresent(config) ? config : new ChangeDetectorGenConfig(assertionsEnabled(),
160+
assertionsEnabled(), false);
161+
}
162+
150163
static isSupported(): boolean { return JitProtoChangeDetector.isSupported(); }
151164

152165
getProtoChangeDetector(id: string, definition: ChangeDetectorDefinition): ProtoChangeDetector {
153166
return new JitProtoChangeDetector(definition);
154167
}
155168

169+
get genConfig(): ChangeDetectorGenConfig { return this._genConfig; }
156170
get generateDetectors(): boolean { return true; }
157-
158-
get genConfig(): ChangeDetectorGenConfig {
159-
return new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
160-
}
161171
}

modules/angular2/src/change_detection/change_detection_jit_generator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class ChangeDetectorJITGenerator {
8282
return new ${this._typeName}(dispatcher);
8383
}
8484
`;
85-
8685
return new Function(ABSTRACT_CHANGE_DETECTOR, UTIL, classDefinition)(AbstractChangeDetector,
8786
ChangeDetectionUtil);
8887
}
@@ -301,6 +300,7 @@ export class ChangeDetectorJITGenerator {
301300

302301
var newValue = this._names.getLocalName(r.selfIndex);
303302
var oldValue = this._names.getFieldName(r.selfIndex);
303+
var notifyDebug = this.genConfig.logBindingUpdate ? `this.logBindingUpdate(${newValue});` : "";
304304

305305
var br = r.bindingRecord;
306306
if (br.target.isDirective()) {
@@ -309,12 +309,14 @@ export class ChangeDetectorJITGenerator {
309309
return `
310310
${this._genThrowOnChangeCheck(oldValue, newValue)}
311311
${directiveProperty} = ${newValue};
312+
${notifyDebug}
312313
${IS_CHANGED_LOCAL} = true;
313314
`;
314315
} else {
315316
return `
316317
${this._genThrowOnChangeCheck(oldValue, newValue)}
317318
this.notifyDispatcher(${newValue});
319+
${notifyDebug}
318320
`;
319321
}
320322
}

modules/angular2/src/change_detection/dynamic_change_detector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
179179
var directiveIndex = bindingRecord.directiveRecord.directiveIndex;
180180
bindingRecord.setter(this._getDirectiveFor(directiveIndex), change.currentValue);
181181
}
182+
183+
if (this.genConfig.logBindingUpdate) {
184+
super.logBindingUpdate(change.currentValue);
185+
}
182186
}
183187

184188
_addChange(bindingRecord: BindingRecord, change, changes) {

modules/angular2/src/change_detection/interfaces.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {ChangeDetectorRef} from './change_detector_ref';
2727
*
2828
* # Example
2929
* ```javascript
30-
* bootstrap(MyApp, [bind(ChangeDetection).toClass(DynamicChangeDetection)]);
30+
* bootstrap(MyApp, [bind(ChangeDetection).toValue(new DynamicChangeDetection())]);
3131
* ```
3232
*/
3333
@CONST()
@@ -49,6 +49,7 @@ export class DebugContext {
4949
export interface ChangeDispatcher {
5050
getDebugContext(elementIndex: number, directiveIndex: DirectiveIndex): DebugContext;
5151
notifyOnBinding(bindingTarget: BindingTarget, value: any): void;
52+
logBindingUpdate(bindingTarget: BindingTarget, value: any): void;
5253
notifyOnAllChangesDone(): void;
5354
}
5455

@@ -74,7 +75,8 @@ export interface ChangeDetector {
7475
export interface ProtoChangeDetector { instantiate(dispatcher: ChangeDispatcher): ChangeDetector; }
7576

7677
export class ChangeDetectorGenConfig {
77-
constructor(public genCheckNoChanges: boolean, public genDebugInfo: boolean) {}
78+
constructor(public genCheckNoChanges: boolean, public genDebugInfo: boolean,
79+
public logBindingUpdate: boolean) {}
7880
}
7981

8082
export class ChangeDetectorDefinition {

modules/angular2/src/core/application_common.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import {Renderer, RenderCompiler} from 'angular2/src/render/api';
6060
import {
6161
DomRenderer,
6262
DOCUMENT,
63-
DOM_REFLECT_PROPERTIES_AS_ATTRIBUTES,
6463
DefaultDomCompiler,
6564
APP_ID_RANDOM_BINDING,
6665
MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE,
@@ -84,16 +83,15 @@ var _rootInjector: Injector;
8483
var _rootBindings = [bind(Reflector).toValue(reflector), TestabilityRegistry];
8584

8685
function _injectorBindings(appComponentType): List<Type | Binding | List<any>> {
87-
var bestChangeDetection: Type = DynamicChangeDetection;
86+
var bestChangeDetection = new DynamicChangeDetection();
8887
if (PreGeneratedChangeDetection.isSupported()) {
89-
bestChangeDetection = PreGeneratedChangeDetection;
88+
bestChangeDetection = new PreGeneratedChangeDetection();
9089
} else if (JitChangeDetection.isSupported()) {
91-
bestChangeDetection = JitChangeDetection;
90+
bestChangeDetection = new JitChangeDetection();
9291
}
9392
return [
9493
bind(DOCUMENT)
9594
.toValue(DOM.defaultDoc()),
96-
bind(DOM_REFLECT_PROPERTIES_AS_ATTRIBUTES).toValue(false),
9795
bind(APP_COMPONENT).toValue(appComponentType),
9896
bind(APP_COMPONENT_REF_PROMISE)
9997
.toFactory(
@@ -141,7 +139,7 @@ function _injectorBindings(appComponentType): List<Type | Binding | List<any>> {
141139
DEFAULT_PIPES,
142140
bind(IterableDiffers).toValue(defaultIterableDiffers),
143141
bind(KeyValueDiffers).toValue(defaultKeyValueDiffers),
144-
bind(ChangeDetection).toClass(bestChangeDetection),
142+
bind(ChangeDetection).toValue(bestChangeDetection),
145143
ViewLoader,
146144
DirectiveResolver,
147145
PipeResolver,

modules/angular2/src/core/compiler/view.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ import {RenderEventDispatcher} from 'angular2/src/render/api';
3232
import {ViewRef, ProtoViewRef, internalView} from './view_ref';
3333
import {ElementRef} from './element_ref';
3434
import {ProtoPipes} from 'angular2/src/core/pipes/pipes';
35+
import {camelCaseToDashCase} from 'angular2/src/render/dom/util';
3536

3637
export {DebugContext} from 'angular2/src/change_detection/interfaces';
3738

39+
const REFLECT_PREFIX: string = 'ng-reflect-';
40+
3841
export class AppProtoViewMergeMapping {
3942
renderProtoViewRef: renderApi.RenderProtoViewRef;
4043
renderFragmentCount: number;
@@ -193,6 +196,14 @@ export class AppView implements ChangeDispatcher, RenderEventDispatcher {
193196
}
194197
}
195198

199+
logBindingUpdate(b: BindingTarget, value: any): void {
200+
if (b.isDirective() || b.isElementProperty()) {
201+
var elementRef = this.elementRefs[this.elementOffset + b.elementIndex];
202+
this.renderer.setElementAttribute(
203+
elementRef, `${REFLECT_PREFIX}${camelCaseToDashCase(b.name)}`, `${value}`);
204+
}
205+
}
206+
196207
notifyOnAllChangesDone(): void {
197208
var eiCount = this.proto.elementBinders.length;
198209
var ei = this.elementInjectors;

0 commit comments

Comments
 (0)
X Tutup