Skip to content

Commit d058102

Browse files
authored
Merge pull request #1530 from ghiscoding/bugfix/preload-destroy
fix: Row Detail preload comp should call destroy lifecycle, fixes #589
2 parents 578d667 + 972547b commit d058102

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnDestroy } from '@angular/core';
22

33
@Component({
4-
template: `<h4>
5-
<i class="mdi mdi-sync mdi-spin-1s mdi-50px"></i>
6-
Loading...
7-
</h4>`,
4+
template: `<div class="container-fluid d-flex align-items-center" style="margin-top: 10px">
5+
<i class="mdi mdi-sync mdi-spin mdi-50px"></i>
6+
<h4>Loading...</h4>
7+
</div>`,
88
})
9-
export class RowDetailPreloadComponent {}
9+
export class RowDetailPreloadComponent implements OnDestroy {
10+
ngOnDestroy(): void {
11+
console.log('preload destroyed');
12+
}
13+
}

src/app/modules/angular-slickgrid/extensions/__tests__/slickRowDetailView.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ describe('SlickRowDetailView', () => {
284284
const onBeforeRowSpy = jest.spyOn(gridOptionsMock.rowDetailView as RowDetailView, 'onBeforeRowDetailToggle');
285285
const onRowOutViewSpy = jest.spyOn(gridOptionsMock.rowDetailView as RowDetailView, 'onRowOutOfViewportRange');
286286
const onRowBackViewSpy = jest.spyOn(gridOptionsMock.rowDetailView as RowDetailView, 'onRowBackToViewportRange');
287+
const appendSpy = jest
288+
.spyOn(angularUtilServiceStub, 'createAngularComponentAppendToDom')
289+
.mockReturnValue({ componentRef: { instance: jest.fn() } } as any);
287290

288291
plugin.init(gridStub);
289292
plugin.onAfterRowDetailToggle = new SlickEvent();
@@ -295,6 +298,7 @@ describe('SlickRowDetailView', () => {
295298
// { notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
296299
// expect.anything()
297300
// );
301+
expect(appendSpy).toHaveBeenCalled();
298302
expect(onAsyncRespSpy).not.toHaveBeenCalled();
299303
expect(onAsyncEndSpy).not.toHaveBeenCalled();
300304
expect(onAfterRowSpy).toHaveBeenCalledWith(expect.anything(), { item: columnsMock[0], expandedRows: [0], grid: gridStub });
@@ -406,7 +410,7 @@ describe('SlickRowDetailView', () => {
406410
const handlerSpy = jest.spyOn(plugin.eventHandler, 'subscribe');
407411
const appendSpy = jest
408412
.spyOn(angularUtilServiceStub, 'createAngularComponentAppendToDom')
409-
.mockReturnValue({ componentRef: { instance: jest.fn() } } as any);
413+
.mockReturnValue({ componentRef: { instance: jest.fn(), destroy: jest.fn() } } as any);
410414

411415
plugin.init(gridStub);
412416
plugin.onBeforeRowDetailToggle = new SlickEvent();

src/app/modules/angular-slickgrid/extensions/slickRowDetailView.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface CreatedView {
3333
export class SlickRowDetailView extends UniversalSlickRowDetailView {
3434
rowDetailContainer!: ViewContainerRef;
3535
protected _preloadComponent: Type<object> | undefined;
36+
protected _preloadCompRef?: ComponentRef<any>;
3637
protected _views: CreatedView[] = [];
3738
protected _viewComponent!: Type<object>;
3839
protected _subscriptions: EventSubscription[] = [];
@@ -154,6 +155,9 @@ export class SlickRowDetailView extends UniversalSlickRowDetailView {
154155

155156
if (this.onAsyncEndUpdate) {
156157
this.eventHandler.subscribe(this.onAsyncEndUpdate, (e, args) => {
158+
// destroy preload if exists
159+
this._preloadCompRef?.destroy();
160+
157161
// triggers after backend called "onAsyncResponse.notify()"
158162
this.renderViewModel(args?.item);
159163

@@ -269,10 +273,11 @@ export class SlickRowDetailView extends UniversalSlickRowDetailView {
269273
`${PRELOAD_CONTAINER_PREFIX}`
270274
) as HTMLCollectionOf<HTMLElement>;
271275
if (this._preloadComponent && containerElements?.length >= 0) {
272-
this.angularUtilService.createAngularComponentAppendToDom(
276+
const preloadComp = this.angularUtilService.createAngularComponentAppendToDom(
273277
this._preloadComponent,
274278
containerElements[containerElements.length - 1]
275279
);
280+
this._preloadCompRef = preloadComp.componentRef;
276281
}
277282
}
278283

@@ -282,6 +287,7 @@ export class SlickRowDetailView extends UniversalSlickRowDetailView {
282287
`${ROW_DETAIL_CONTAINER_PREFIX}${item[this.datasetIdPropName]}`
283288
) as HTMLCollectionOf<HTMLElement>;
284289
if (this._viewComponent && containerElements?.length > 0) {
290+
// render row detail
285291
const componentOutput = this.angularUtilService.createAngularComponentAppendToDom(
286292
this._viewComponent,
287293
containerElements[containerElements.length - 1],

0 commit comments

Comments
 (0)