diff --git a/src/abstract/Module.ts b/src/abstract/Module.ts index e42a9d751..07fd61883 100644 --- a/src/abstract/Module.ts +++ b/src/abstract/Module.ts @@ -1,5 +1,5 @@ import { isElement, isUndefined, isString } from 'underscore'; -import { Collection, View } from '../common'; +import { Collection, Debounced, Model, View } from '../common'; import { EditorConfigKeys } from '../editor/config/config'; import EditorModel from '../editor/model/Editor'; import { ProjectData } from '../storage_manager/model/IStorage'; @@ -39,7 +39,10 @@ export default abstract class Module impl private _em: EditorModel; private _config: T & { pStylePrefix?: string }; private _name: string; + debounced: Debounced[] = []; + collections: Collection[] = []; cls: any[] = []; + state?: Model; events: any; model?: any; view?: any; @@ -67,7 +70,6 @@ export default abstract class Module impl return this._config; } - abstract destroy(): void; render(opts?: any): HTMLElement | JQuery | void {} postLoad(key: any): void {} @@ -88,6 +90,21 @@ export default abstract class Module impl postRender?(view: any): void; + destroy() { + this.__destroy(); + } + + __destroy() { + this.view?.remove(); + this.state?.stopListening(); + this.state?.clear(); + this.debounced.forEach(d => d.cancel()); + this.collections.forEach(c => { + c.stopListening(); + c.reset(); + }); + } + /** * Move the main DOM element of the module. * To execute only post editor render (in postRender) diff --git a/src/trait_manager/index.ts b/src/trait_manager/index.ts index 5725bd795..0b3022e9e 100644 --- a/src/trait_manager/index.ts +++ b/src/trait_manager/index.ts @@ -59,12 +59,12 @@ export default class TraitManager extends Module { ppfx && (config.stylePrefix = `${ppfx}${config.stylePrefix}`); const upAll = debounce(() => this.__upSel(), 0); - state.listenTo(em, 'component:toggled', upAll); - const update = debounce(() => this.__onUp(), 0); + state.listenTo(em, 'component:toggled', upAll); state.listenTo(em, 'trait:update', update); - return this; + this.debounced = [upAll, update]; + this.collections = [this.categories as any]; } /** @@ -148,17 +148,6 @@ export default class TraitManager extends Module { return view.el; } - destroy() { - const colls = [this.categories]; - colls.forEach(c => { - c.stopListening(); - c.reset(); - }); - this.state.stopListening(); - this.state.clear(); - this.view?.remove(); - } - postRender() { this.__appendTo(); }