Skip to content

Commit

Permalink
fix(core): properly remove event listeners when disposing (#163)
Browse files Browse the repository at this point in the history
* fix(core): properly remove event listeners when disposing
  • Loading branch information
ghiscoding authored Nov 19, 2020
1 parent 0ecbc7e commit ecfb9a7
Show file tree
Hide file tree
Showing 47 changed files with 967 additions and 836 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
*.map
*.md
*.zip
*.spec.ts
**/dist/*.*
24 changes: 0 additions & 24 deletions examples/webpack-demo-vanilla-bundle/src/examples/event.service.ts

This file was deleted.

13 changes: 7 additions & 6 deletions examples/webpack-demo-vanilla-bundle/src/examples/example02.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Aggregators,
BindingEventService,
Column,
FieldType,
Filters,
Expand All @@ -11,7 +12,7 @@ import {
SlickDataView,
SlickGrid,
SortComparers,
SortDirectionNumber
SortDirectionNumber,
} from '@slickgrid-universal/common';
import { ExcelExportService } from '@slickgrid-universal/excel-export';
import { FileExportService } from '@slickgrid-universal/file-export';
Expand All @@ -20,12 +21,11 @@ import { Slicker, SlickerGridInstance, SlickVanillaGridBundle } from '@slickgrid
import { ExampleGridOptions } from './example-grid-options';
import '../material-styles.scss';
import './example02.scss';
import { EventService } from './event.service';

const NB_ITEMS = 500;

export class Example2 {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column[];
gridOptions: GridOption;
dataset: any[];
Expand All @@ -44,16 +44,16 @@ export class Example2 {
}

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
}

attached() {
this.initializeGrid();
this.dataset = this.loadData(NB_ITEMS);
const gridContainerElm = document.querySelector<HTMLDivElement>(`.grid2`);

this.eventService.addElementEventListener(gridContainerElm, 'onbeforeexporttoexcel', () => console.log('onBeforeExportToExcel'));
this.eventService.addElementEventListener(gridContainerElm, 'onafterexporttoexcel', () => console.log('onAfterExportToExcel'));
this._bindingEventService.bind(gridContainerElm, 'onbeforeexporttoexcel', () => console.log('onBeforeExportToExcel'));
this._bindingEventService.bind(gridContainerElm, 'onafterexporttoexcel', () => console.log('onAfterExportToExcel'));
this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, this.dataset);

// you could group by duration on page load (must be AFTER the DataView is created, so after GridBundle)
Expand All @@ -62,6 +62,7 @@ export class Example2 {

dispose() {
this.sgb?.dispose();
this._bindingEventService.unbindAll();
}

initializeGrid() {
Expand Down
18 changes: 9 additions & 9 deletions examples/webpack-demo-vanilla-bundle/src/examples/example03.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Aggregators,
BindingEventService,
Column,
Editors,
FieldType,
Expand All @@ -22,7 +23,6 @@ import { Slicker, SlickerGridInstance, SlickVanillaGridBundle } from '@slickgrid
import { ExampleGridOptions } from './example-grid-options';
import '../salesforce-styles.scss';
import './example03.scss';
import { EventService } from './event.service';

// using external SlickGrid JS libraries
declare const Slick: SlickNamespace;
Expand All @@ -38,7 +38,7 @@ interface ReportItem {
}

export class Example3 {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column<ReportItem>[];
gridOptions: GridOption;
dataset: any[];
Expand All @@ -52,25 +52,25 @@ export class Example3 {
selectedGroupingFields: Array<string | GroupingGetterFunction> = ['', '', ''];

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
}

attached() {
this.initializeGrid();
this.dataset = this.loadData(500);
const gridContainerElm = document.querySelector<HTMLDivElement>(`.grid3`);

this.eventService.addElementEventListener(gridContainerElm, 'onclick', this.handleOnClick.bind(this));
this.eventService.addElementEventListener(gridContainerElm, 'oncellchange', this.handleOnCellChange.bind(this));
this.eventService.addElementEventListener(gridContainerElm, 'onvalidationerror', this.handleValidationError.bind(this));
this.eventService.addElementEventListener(gridContainerElm, 'onitemdeleted', this.handleItemDeleted.bind(this));
this.eventService.addElementEventListener(gridContainerElm, 'onslickergridcreated', this.handleOnSlickerGridCreated.bind(this));
this._bindingEventService.bind(gridContainerElm, 'onclick', this.handleOnClick.bind(this));
this._bindingEventService.bind(gridContainerElm, 'oncellchange', this.handleOnCellChange.bind(this));
this._bindingEventService.bind(gridContainerElm, 'onvalidationerror', this.handleValidationError.bind(this));
this._bindingEventService.bind(gridContainerElm, 'onitemdeleted', this.handleItemDeleted.bind(this));
this._bindingEventService.bind(gridContainerElm, 'onslickergridcreated', this.handleOnSlickerGridCreated.bind(this));
this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, this.dataset);
}

dispose() {
this.sgb?.dispose();
this.eventService.unbindAllEvents();
this._bindingEventService.unbindAll();
}

initializeGrid() {
Expand Down
14 changes: 7 additions & 7 deletions examples/webpack-demo-vanilla-bundle/src/examples/example04.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AutocompleteOption,
BindingEventService,
Column,
ColumnEditorDualInput,
Editors,
Expand All @@ -13,7 +14,6 @@ import {
} from '@slickgrid-universal/common';
import { ExcelExportService } from '@slickgrid-universal/excel-export';
import { Slicker, SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle';
import { EventService } from './event.service';
import { ExampleGridOptions } from './example-grid-options';
import './example02.scss';

Expand Down Expand Up @@ -42,7 +42,7 @@ const customEditableInputFormatter = (_row: number, _cell: number, _value: any,
};

export class Example4 {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column<ReportItem>[];
gridOptions: GridOption;
dataset: any[];
Expand All @@ -55,22 +55,22 @@ export class Example4 {
sgb: SlickVanillaGridBundle;

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
}

attached() {
const dataset = this.initializeGrid();
const gridContainerElm = document.querySelector<HTMLDivElement>(`.grid4`);

// this.eventService.addElementEventListener(gridContainerElm, 'onclick', handleOnClick);
this.eventService.addElementEventListener(gridContainerElm, 'onvalidationerror', this.handleOnValidationError.bind(this));
this.eventService.addElementEventListener(gridContainerElm, 'onitemdeleted', this.handleOnItemDeleted.bind(this));
// this._bindingEventService.bind(gridContainerElm, 'onclick', handleOnClick);
this._bindingEventService.bind(gridContainerElm, 'onvalidationerror', this.handleOnValidationError.bind(this));
this._bindingEventService.bind(gridContainerElm, 'onitemdeleted', this.handleOnItemDeleted.bind(this));
this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, dataset);
}

dispose() {
this.sgb?.dispose();
this.eventService.unbindAllEvents();
this._bindingEventService.unbindAll();
}

initializeGrid() {
Expand Down
11 changes: 6 additions & 5 deletions examples/webpack-demo-vanilla-bundle/src/examples/example07.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BindingEventService,
Column,
Editors,
Filters,
Expand All @@ -8,33 +9,33 @@ import {
} from '@slickgrid-universal/common';
import { ExcelExportService } from '@slickgrid-universal/excel-export';
import { Slicker, SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle';
import { EventService } from './event.service';

import { ExampleGridOptions } from './example-grid-options';

export class Example7 {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column[];
gridOptions: GridOption;
dataset: any[];
sgb: SlickVanillaGridBundle;
duplicateTitleHeaderCount = 1;

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
}

attached() {
this.initializeGrid();
this.dataset = this.loadData(500);
const gridContainerElm = document.querySelector<HTMLDivElement>(`.grid7`);
this.eventService.addElementEventListener(gridContainerElm, 'oncellchange', this.handleOnCellChange.bind(this));
this.eventService.addElementEventListener(gridContainerElm, 'onvalidationerror', this.handleValidationError.bind(this));
this._bindingEventService.bind(gridContainerElm, 'oncellchange', this.handleOnCellChange.bind(this));
this._bindingEventService.bind(gridContainerElm, 'onvalidationerror', this.handleValidationError.bind(this));
this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, this.dataset);
}

dispose() {
this.sgb?.dispose();
this._bindingEventService.unbindAll();
}

initializeGrid() {
Expand Down
14 changes: 7 additions & 7 deletions examples/webpack-demo-vanilla-bundle/src/examples/example09.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Column, FieldType, Filters, GridOption, GridStateChange, Metrics, OperatorType, } from '@slickgrid-universal/common';
import { BindingEventService, Column, FieldType, Filters, GridOption, GridStateChange, Metrics, OperatorType, } from '@slickgrid-universal/common';
import { GridOdataService, OdataServiceApi, OdataOption } from '@slickgrid-universal/odata';
import { Slicker, SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle';
import { EventService } from './event.service';
import { ExampleGridOptions } from './example-grid-options';

const defaultPageSize = 20;

export class Example09 {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column[];
gridOptions: GridOption;
metrics: Metrics;
Expand All @@ -21,23 +20,24 @@ export class Example09 {
statusClass = 'is-success';

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
}

attached() {
this.initializeGrid();
const gridContainerElm = document.querySelector<HTMLDivElement>(`.grid9`);

this.eventService.addElementEventListener(gridContainerElm, 'ongridstatechanged', this.gridStateChanged.bind(this));
// this.eventService.addElementEventListener(gridContainerElm, 'onbeforeexporttoexcel', () => console.log('onBeforeExportToExcel'));
// this.eventService.addElementEventListener(gridContainerElm, 'onafterexporttoexcel', () => console.log('onAfterExportToExcel'));
this._bindingEventService.bind(gridContainerElm, 'ongridstatechanged', this.gridStateChanged.bind(this));
// this._bindingEventService.bind(gridContainerElm, 'onbeforeexporttoexcel', () => console.log('onBeforeExportToExcel'));
// this._bindingEventService.bind(gridContainerElm, 'onafterexporttoexcel', () => console.log('onAfterExportToExcel'));
this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, []);
}

dispose() {
if (this.sgb) {
this.sgb?.dispose();
}
this._bindingEventService.unbindAll();
}

initializeGrid() {
Expand Down
12 changes: 6 additions & 6 deletions examples/webpack-demo-vanilla-bundle/src/examples/example10.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BindingEventService,
Column,
FieldType,
Filters,
Expand All @@ -16,13 +17,12 @@ import * as moment from 'moment-mini';
import { ExampleGridOptions } from './example-grid-options';
import { TranslateService } from '../translate.service';
import './example10.scss';
import { EventService } from './event.service';

const defaultPageSize = 20;
const GRAPHQL_QUERY_DATASET_NAME = 'users';

export class Example10 {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column[];
gridOptions: GridOption;
dataset = [];
Expand All @@ -39,7 +39,7 @@ export class Example10 {
translateService: TranslateService;

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
// get the Translate Service from the window object,
// it might be better with proper Dependency Injection but this project doesn't have any at this point
this.translateService = (<any>window).TranslateService;
Expand All @@ -52,16 +52,16 @@ export class Example10 {
if (this.sgb) {
this.sgb?.dispose();
}
this.eventService.unbindAllEvents();
this._bindingEventService.unbindAll();
// this.saveCurrentGridState();
}

attached() {
this.initializeGrid();
const gridContainerElm = document.querySelector<HTMLDivElement>(`.grid10`);

// this.eventService.addElementEventListener(gridContainerElm, 'onbeforeexporttoexcel', () => console.log('onBeforeExportToExcel'));
// this.eventService.addElementEventListener(gridContainerElm, 'onafterexporttoexcel', () => console.log('onAfterExportToExcel'));
// this._bindingEventService.bind(gridContainerElm, 'onbeforeexporttoexcel', () => console.log('onBeforeExportToExcel'));
// this._bindingEventService.bind(gridContainerElm, 'onafterexporttoexcel', () => console.log('onAfterExportToExcel'));
this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, this.dataset);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {
BindingEventService,
Column,
DOMEvent,
emptyElement,
Formatter,
Formatters,
GridOption,
emptyElement,
} from '@slickgrid-universal/common';
import { Slicker, SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle';

import { ExampleGridOptions } from './example-grid-options';
import '../salesforce-styles.scss';
import './example11-modal.scss';
import { EventService } from './event.service';

export class Example11Modal {
private eventService: EventService;
private _bindingEventService: BindingEventService;
columnDefinitions: Column[];
gridOptions: GridOption;
sgb: SlickVanillaGridBundle;
Expand All @@ -22,7 +23,7 @@ export class Example11Modal {
selectedIds: string[] = [];

constructor() {
this.eventService = new EventService();
this._bindingEventService = new BindingEventService();
}

attached() {
Expand All @@ -35,7 +36,7 @@ export class Example11Modal {
if (bindings.columnDefinitions) {
this.columnDefinitions = bindings.columnDefinitions;
this.gridContainerElm = document.querySelector<HTMLDivElement>(`.modal-grid`);
this.eventService.addElementEventListener(this.gridContainerElm, 'onvalidationerror', this.handleValidationError.bind(this));
this._bindingEventService.bind(this.gridContainerElm, 'onvalidationerror', this.handleValidationError.bind(this));

const dataset = [this.createEmptyItem(bindings.columnDefinitions)];
this.sgb = new Slicker.GridBundle(this.gridContainerElm, this.columnDefinitions, { ...ExampleGridOptions, ...this.gridOptions }, dataset);
Expand All @@ -50,7 +51,7 @@ export class Example11Modal {

dispose() {
this.sgb?.dispose();
this.eventService.unbindAllEvents();
this._bindingEventService.unbindAll();
this.gridContainerElm = null;
}

Expand Down Expand Up @@ -144,7 +145,7 @@ export class Example11Modal {
private bindCloseBulmaModal(callback?: () => void) {
const modalCloseBtnElms = document.querySelectorAll<HTMLButtonElement>('.close, .delete, .modal-close');

window.addEventListener('click', (event: any) => {
window.addEventListener('click', (event: DOMEvent<HTMLInputElement>) => {
if (event.target.className === 'modal-background') {
this.closeBulmaModal(callback);
}
Expand Down
Loading

0 comments on commit ecfb9a7

Please sign in to comment.