diff --git a/modules/angular2/src/change_detection/abstract_change_detector.ts b/modules/angular2/src/change_detection/abstract_change_detector.ts index 4e7ae6d502..7fab994116 100644 --- a/modules/angular2/src/change_detection/abstract_change_detector.ts +++ b/modules/angular2/src/change_detection/abstract_change_detector.ts @@ -67,7 +67,13 @@ export class AbstractChangeDetector implements ChangeDetector { remove(): void { this.parent.removeChild(this); } handleEvent(eventName: string, elIndex: number, locals: Locals): boolean { - throw new BaseException("Not implemented"); + var res = this.handleEventInternal(eventName, elIndex, locals); + this.markPathToRootAsCheckOnce(); + return res; + } + + handleEventInternal(eventName: string, elIndex: number, locals: Locals): boolean { + return false; } detectChanges(): void { this.runDetectChanges(false); } diff --git a/modules/angular2/src/change_detection/change_detection_jit_generator.ts b/modules/angular2/src/change_detection/change_detection_jit_generator.ts index ebe76dd9c7..d4b4946016 100644 --- a/modules/angular2/src/change_detection/change_detection_jit_generator.ts +++ b/modules/angular2/src/change_detection/change_detection_jit_generator.ts @@ -50,13 +50,6 @@ export class ChangeDetectorJITGenerator { ${this._typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype); - ${this._typeName}.prototype.handleEvent = function(eventName, elIndex, locals) { - var ${this._names.getPreventDefaultAccesor()} = false; - ${this._names.genInitEventLocals()} - ${this._genHandleEvent()} - return ${this._names.getPreventDefaultAccesor()}; - } - ${this._typeName}.prototype.detectChangesInRecordsInternal = function(throwOnChange) { ${this._names.genInitLocals()} var ${IS_CHANGED_LOCAL} = false; @@ -67,6 +60,8 @@ export class ChangeDetectorJITGenerator { ${this._names.getAlreadyCheckedName()} = true; } + ${this._maybeGenHandleEventInternal()} + ${this._genCheckNoChanges()} ${this._maybeGenCallOnAllChangesDone()} @@ -84,8 +79,20 @@ export class ChangeDetectorJITGenerator { AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords); } - _genHandleEvent(): string { - return this.eventBindings.map(eb => this._genEventBinding(eb)).join("\n"); + _maybeGenHandleEventInternal(): string { + if (this.eventBindings.length > 0) { + var handlers = this.eventBindings.map(eb => this._genEventBinding(eb)).join("\n"); + return ` + ${this._typeName}.prototype.handleEvent = function(eventName, elIndex, locals) { + var ${this._names.getPreventDefaultAccesor()} = false; + ${this._names.genInitEventLocals()} + ${handlers} + return ${this._names.getPreventDefaultAccesor()}; + } + `; + } else { + return ''; + } } _genEventBinding(eb: EventBinding): string { diff --git a/modules/angular2/src/change_detection/dynamic_change_detector.ts b/modules/angular2/src/change_detection/dynamic_change_detector.ts index 8c77f261b0..53e14f1de7 100644 --- a/modules/angular2/src/change_detection/dynamic_change_detector.ts +++ b/modules/angular2/src/change_detection/dynamic_change_detector.ts @@ -31,7 +31,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector { this.dehydrateDirectives(false); } - handleEvent(eventName: string, elIndex: number, locals: Locals): boolean { + handleEventInternal(eventName: string, elIndex: number, locals: Locals): boolean { var preventDefault = false; this._matchingEventBindings(eventName, elIndex) @@ -41,6 +41,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector { preventDefault = true; } }); + return preventDefault; } diff --git a/modules/angular2/src/transform/template_compiler/change_detector_codegen.dart b/modules/angular2/src/transform/template_compiler/change_detector_codegen.dart index 482ea08fad..f075b03363 100644 --- a/modules/angular2/src/transform/template_compiler/change_detector_codegen.dart +++ b/modules/angular2/src/transform/template_compiler/change_detector_codegen.dart @@ -125,13 +125,6 @@ class _CodegenState { dehydrateDirectives(false); } - bool handleEvent(eventName, elIndex, locals) { - var ${_names.getPreventDefaultAccesor()} = false; - ${_names.genInitEventLocals()} - ${_genHandleEvent()} - return ${this._names.getPreventDefaultAccesor()}; - } - void detectChangesInRecordsInternal(throwOnChange) { ${_names.genInitLocals()} var $_IS_CHANGED_LOCAL = false; @@ -142,6 +135,8 @@ class _CodegenState { ${_names.getAlreadyCheckedName()} = true; } + ${_maybeGenHandleEventInternal()} + ${_genCheckNoChanges()} ${_maybeGenCallOnAllChangesDone()} @@ -161,8 +156,20 @@ class _CodegenState { '''); } - String _genHandleEvent() { - return _eventBindings.map((eb) => _genEventBinding(eb)).join("\n"); + String _maybeGenHandleEventInternal() { + if (_eventBindings.length > 0) { + var handlers = _eventBindings.map((eb) => _genEventBinding(eb)).join("\n"); + return ''' + handleEventInternal(eventName, elIndex, locals) { + var ${this._names.getPreventDefaultAccesor()} = false; + ${this._names.genInitEventLocals()} + ${handlers} + return ${this._names.getPreventDefaultAccesor()}; + } + '''; + } else { + return ''; + } } String _genEventBinding(EventBinding eb) { diff --git a/modules/angular2/test/change_detection/change_detector_spec.ts b/modules/angular2/test/change_detection/change_detector_spec.ts index c67c6079a3..1c032e0b87 100644 --- a/modules/angular2/test/change_detection/change_detector_spec.ts +++ b/modules/angular2/test/change_detection/change_detector_spec.ts @@ -899,6 +899,7 @@ export function main() { res = val.changeDetector.handleEvent("event", 0, locals); expect(res).toBe(false); }); + }); }); }); diff --git a/modules/angular2/test/transform/integration/two_annotations_files/expected/bar.ng_deps.dart b/modules/angular2/test/transform/integration/two_annotations_files/expected/bar.ng_deps.dart index 9211864be2..1e63908547 100644 --- a/modules/angular2/test/transform/integration/two_annotations_files/expected/bar.ng_deps.dart +++ b/modules/angular2/test/transform/integration/two_annotations_files/expected/bar.ng_deps.dart @@ -36,11 +36,6 @@ class _MyComponent_ChangeDetector0 dehydrateDirectives(false); } - bool handleEvent(eventName, elIndex, locals) { - var preventDefault = false; - return preventDefault; - } - void detectChangesInRecordsInternal(throwOnChange) { var l_context = this.context, l_myNum0, c_myNum0, l_interpolate1; c_myNum0 = false;