Skip to content

Commit e08eac4

Browse files
committed
Destroy the chart on disconnect
1 parent b68bff8 commit e08eac4

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/Chartjs/assets/dist/controller.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default class extends Controller {
55
view: ObjectConstructor;
66
};
77
private chart;
8-
initialize(): void;
8+
connect(): void;
9+
disconnect(): void;
910
viewValueChanged(): void;
1011
private dispatchEvent;
1112
}

src/Chartjs/assets/dist/controller.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class default_1 extends Controller {
1010
super(...arguments);
1111
this.chart = null;
1212
}
13-
initialize() {
13+
connect() {
1414
if (!isChartInitialized) {
1515
isChartInitialized = true;
1616
this.dispatchEvent('init', {
@@ -35,6 +35,13 @@ class default_1 extends Controller {
3535
this.chart = new Chart(canvasContext, payload);
3636
this.dispatchEvent('connect', { chart: this.chart });
3737
}
38+
disconnect() {
39+
if (this.chart) {
40+
this.chart.destroy();
41+
this.chart = null;
42+
}
43+
this.dispatchEvent('disconnect', { chart: this.chart });
44+
}
3845
viewValueChanged() {
3946
if (this.chart) {
4047
const viewValue = { data: this.viewValue.data, options: this.viewValue.options };

src/Chartjs/assets/src/controller.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class extends Controller {
2626

2727
private chart: Chart | null = null;
2828

29-
initialize() {
29+
connect() {
3030
if (!isChartInitialized) {
3131
isChartInitialized = true;
3232
this.dispatchEvent('init', {
@@ -57,6 +57,15 @@ export default class extends Controller {
5757
this.dispatchEvent('connect', { chart: this.chart });
5858
}
5959

60+
disconnect() {
61+
if (this.chart) {
62+
this.chart.destroy();
63+
this.chart = null;
64+
}
65+
66+
this.dispatchEvent('disconnect', { chart: this.chart });
67+
}
68+
6069
/**
6170
* If the underlying data or options change, let's update the chart!
6271
*/

0 commit comments

Comments
 (0)