Skip to content

Commit

Permalink
fix: destroying event listener on window
Browse files Browse the repository at this point in the history
  • Loading branch information
scmmishra committed Aug 11, 2019
1 parent 884c52c commit 92674f9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ <h6>Available options</h6>
chart.export();

// Unbind window-resize events
chart.unbindWindowEvents();
chart.destroy();

</code></pre>
</section>
Expand Down
18 changes: 6 additions & 12 deletions src/js/charts/BaseChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { getColor, isValidColor } from '../utils/colors';
import { runSMILAnimation } from '../utils/animation';
import { downloadFile, prepareForExport } from '../utils/export';

let BOUND_DRAW_FN;

export default class BaseChart {
constructor(parent, options) {

Expand Down Expand Up @@ -90,18 +88,14 @@ export default class BaseChart {
this.height = height - getExtraHeight(this.measures);

// Bind window events
BOUND_DRAW_FN = this.boundDrawFn.bind(this);
window.addEventListener('resize', BOUND_DRAW_FN);
window.addEventListener('orientationchange', this.boundDrawFn.bind(this));
}

boundDrawFn() {
this.draw(true);
this.boundDrawFn = () => this.draw(true);
window.addEventListener('resize', this.boundDrawFn);
window.addEventListener('orientationchange', this.boundDrawFn);
}

unbindWindowEvents() {
window.removeEventListener('resize', BOUND_DRAW_FN);
window.removeEventListener('orientationchange', this.boundDrawFn.bind(this));
destroy() {
window.removeEventListener('resize', this.boundDrawFn);
window.removeEventListener('orientationchange', this.boundDrawFn);
}

// Has to be called manually
Expand Down

0 comments on commit 92674f9

Please sign in to comment.