X Tutup
Skip to content

Commit 280b86e

Browse files
tboschmatsko
authored andcommitted
fix(change_detection): allow to destroy OnPush components inside of a host event.
1 parent 2f5a2ba commit 280b86e

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
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) => {
@@ -1953,6 +1977,16 @@ class PushCmpWithRef {
19531977
propagate() { this.ref.markForCheck(); }
19541978
}
19551979

1980+
@Component({
1981+
selector: 'push-cmp-with-host-event',
1982+
host: {'(click)': 'ctxCallback($event)'},
1983+
changeDetection: ChangeDetectionStrategy.OnPush,
1984+
template: ''
1985+
})
1986+
class PushCmpWithHostEvent {
1987+
ctxCallback: Function = (_) => {};
1988+
}
1989+
19561990
@Component({
19571991
selector: 'push-cmp-with-async',
19581992
changeDetection: ChangeDetectionStrategy.OnPush,

0 commit comments

Comments
 (0)
X Tutup