Skip to content

Commit 0e504eb

Browse files
authored
Merge pull request #325 from DevExpress/memory_leaks
Dispose widgets only once
2 parents d5bd492 + 8f6cb1d commit 0e504eb

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

src/core/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export abstract class DxComponent implements INestedOptionContainer, ICollection
8888
}
8989
protected _destroyWidget() {
9090
if (this.instance) {
91-
this.instance._dispose();
91+
this.instance.element().triggerHandler({ type: 'dxremove', _angularIntegration: true });
9292
}
9393
}
9494
constructor(protected element: ElementRef, private ngZone: NgZone, templateHost: DxTemplateHost, private watcherHelper: WatcherHelper) {

src/core/template.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export class DxTemplateDirective {
4646
// =========== /WORKAROUND =============
4747
return $(childView.rootNodes)
4848
.addClass(DX_TEMPLATE_WRAPPER_CLASS)
49-
.on('dxremove', () => childView.destroy());
49+
.on('dxremove', (e) => {
50+
if (!e._angularIntegration) {
51+
childView.destroy();
52+
}
53+
});
5054
}
5155
}
5256

tests/src/ui/list.spec.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import DxList from 'devextreme/ui/list';
1616

1717
import {
18+
DxButtonModule,
1819
DxListModule,
1920
DxListComponent
2021
} from '../../../dist';
@@ -325,8 +326,7 @@ describe('DxList', () => {
325326
expect(instance.element().find('.dx-item-content').eq(0).text()).toBe('testTemplate');
326327
}));
327328

328-
329-
it('should destroy all components inside template', () => {
329+
it('should destroy angular components inside template', () => {
330330
let destroyed = false;
331331
@Component({
332332
selector: 'destroyable-component',
@@ -363,4 +363,76 @@ describe('DxList', () => {
363363

364364
expect(destroyed).toBe(true);
365365
});
366+
367+
it('should destroy devextreme components inside template', () => {
368+
@Component({
369+
selector: 'test-container-component',
370+
template: ''
371+
})
372+
class TestContainerComponent {
373+
items = [0];
374+
buttonDestroyed = false;
375+
listVisible = true;
376+
}
377+
378+
TestBed.configureTestingModule({
379+
declarations: [TestContainerComponent],
380+
imports: [DxButtonModule, DxListModule]
381+
});
382+
383+
TestBed.overrideComponent(TestContainerComponent, {
384+
set: {
385+
template: `
386+
<dx-list *ngIf="listVisible" [items]="items">
387+
<div *dxTemplate="let item of 'item'">
388+
<dx-button text="Test" (onDisposing)="buttonDestroyed = true"></dx-button>
389+
</div>
390+
</dx-list>
391+
`
392+
}
393+
});
394+
395+
let fixture = TestBed.createComponent(TestContainerComponent);
396+
fixture.detectChanges();
397+
398+
fixture.componentInstance.listVisible = false;
399+
fixture.detectChanges();
400+
401+
expect(fixture.componentInstance.buttonDestroyed).toBe(true);
402+
});
403+
404+
it('should destroy devextreme components in template root correctly', () => {
405+
@Component({
406+
selector: 'test-container-component',
407+
template: ''
408+
})
409+
class TestContainerComponent {
410+
items = [0];
411+
buttonDestroyed = false;
412+
listVisible = true;
413+
}
414+
415+
TestBed.configureTestingModule({
416+
declarations: [TestContainerComponent],
417+
imports: [DxButtonModule, DxListModule]
418+
});
419+
420+
TestBed.overrideComponent(TestContainerComponent, {
421+
set: {
422+
template: `
423+
<dx-list *ngIf="listVisible" [items]="items">
424+
<dx-button text="Test" (onDisposing)="buttonDestroyed = true" *dxTemplate="let item of 'item'"></dx-button>
425+
</dx-list>
426+
`
427+
}
428+
});
429+
430+
let fixture = TestBed.createComponent(TestContainerComponent);
431+
fixture.detectChanges();
432+
433+
fixture.componentInstance.listVisible = false;
434+
fixture.detectChanges();
435+
436+
expect(fixture.componentInstance.buttonDestroyed).toBe(true);
437+
});
366438
});

0 commit comments

Comments
 (0)