X Tutup
Skip to content

Commit ebd438f

Browse files
committed
fix(change_detection): allow to destroy OnPush components inside of a host event.
Closes #7192
1 parent 331b9c1 commit ebd438f

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

modules/angular2/src/core/change_detection/change_detection_jit_generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class ChangeDetectorJITGenerator {
174174
var evalRecord = this._logic.genEventBindingEvalValue(eb, r);
175175
var markPath = this._genMarkPathToRootAsCheckOnce(r);
176176
var prevDefault = this._genUpdatePreventDefault(eb, r);
177-
return `${evalRecord}\n${markPath}\n${prevDefault}`;
177+
return `${markPath}\n${evalRecord}\n${prevDefault}`;
178178
} else {
179179
return this._logic.genEventBindingEvalValue(eb, r);
180180
}

modules/angular2/src/core/change_detection/dynamic_change_detector.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
6060
if (proto.isSkipRecord()) {
6161
protoIdx += this._computeSkipLength(protoIdx, proto, values);
6262
} else {
63-
var res = this._calculateCurrValue(proto, values, locals);
6463
if (proto.lastInBinding) {
6564
this._markPathAsCheckOnce(proto);
65+
}
66+
var res = this._calculateCurrValue(proto, values, locals);
67+
if (proto.lastInBinding) {
6668
return res;
6769
} else {
6870
this._writeSelf(proto, res, values);

modules/angular2/test/core/linker/integration_spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,30 @@ function declareTests() {
755755
async.done();
756756
})}));
757757

758+
if (DOM.supportsDOMEvents()) {
759+
it("should allow to destroy a component from within a host event handler",
760+
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
761+
762+
var fixture: ComponentFixture;
763+
tcb.overrideView(
764+
MyComp, new ViewMetadata({
765+
template: '<push-cmp-with-host-event></push-cmp-with-host-event>',
766+
directives: [[[PushCmpWithHostEvent]]]
767+
}))
768+
769+
.createAsync(MyComp)
770+
.then(root => { fixture = root; });
771+
tick();
772+
fixture.detectChanges();
773+
774+
var cmpEl = fixture.debugElement.children[0];
775+
var cmp: PushCmpWithHostEvent = cmpEl.inject(PushCmpWithHostEvent);
776+
cmp.ctxCallback = (_) => fixture.destroy();
777+
778+
expect(() => cmpEl.triggerEventHandler('click', <Event>{})).not.toThrow();
779+
})));
780+
}
781+
758782
it('should not affect updating properties on the component',
759783
inject([TestComponentBuilder, AsyncTestCompleter],
760784
(tcb: TestComponentBuilder, async) => {
@@ -1995,6 +2019,16 @@ class PushCmpWithRef {
19952019
propagate() { this.ref.markForCheck(); }
19962020
}
19972021

2022+
@Component({
2023+
selector: 'push-cmp-with-host-event',
2024+
host: {'(click)': 'ctxCallback($event)'},
2025+
changeDetection: ChangeDetectionStrategy.OnPush,
2026+
template: ''
2027+
})
2028+
class PushCmpWithHostEvent {
2029+
ctxCallback: Function = (_) => {};
2030+
}
2031+
19982032
@Component({
19992033
selector: 'push-cmp-with-async',
20002034
changeDetection: ChangeDetectionStrategy.OnPush,

modules_dart/transform/lib/src/transform/template_compiler/change_detector_codegen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class _CodegenState {
253253
var evalRecord = _logic.genEventBindingEvalValue(eb, r);
254254
var markPath = _genMarkPathToRootAsCheckOnce(r);
255255
var prevDefault = _genUpdatePreventDefault(eb, r);
256-
return "${evalRecord}\n${markPath}\n${prevDefault}";
256+
return "${markPath}\n${evalRecord}\n${prevDefault}";
257257
} else {
258258
return _logic.genEventBindingEvalValue(eb, r);
259259
}

0 commit comments

Comments
 (0)
X Tutup