From 92c5f414ef8132225bd6677d092335e8b38e5a06 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Mon, 17 Nov 2014 22:54:09 -0500 Subject: [PATCH] Call .destroy() on plugins (fixes #418) --- auto_tests/tests/plugins.js | 21 +++++++++++++++++++++ dygraph.js | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/auto_tests/tests/plugins.js b/auto_tests/tests/plugins.js index 5655585..3605113 100644 --- a/auto_tests/tests/plugins.js +++ b/auto_tests/tests/plugins.js @@ -212,3 +212,24 @@ pluginsTestCase.prototype.testEventSequence = function() { "didDrawChart" ], events); }; + +pluginsTestCase.prototype.testDestroyCalledInOrder = function() { + var destructions = []; + var makePlugin = function(name) { + return { + activate: function(g) { return {} }, + destroy: function() { + destructions.push(name); + } + }; + }; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, this.data, { + plugins: [makePlugin('p'), makePlugin('q')] + }); + + assertEquals([], destructions); + g.destroy(); + assertEquals(['q', 'p'], destructions); +}; diff --git a/dygraph.js b/dygraph.js index 026365b..7dbf29f 100644 --- a/dygraph.js +++ b/dygraph.js @@ -1230,6 +1230,12 @@ Dygraph.prototype.destroy = function() { this.canvas_ctx_.restore(); this.hidden_ctx_.restore(); + // Destroy any plugins, in the reverse order that they were registered. + for (var i = this.plugins_.length - 1; i >= 0; i--) { + var p = this.plugins_.pop(); + if (p.plugin.destroy) p.plugin.destroy(); + } + var removeRecursive = function(node) { while (node.hasChildNodes()) { removeRecursive(node.firstChild); @@ -1244,7 +1250,7 @@ Dygraph.prototype.destroy = function() { Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); // remove window handlers - Dygraph.removeEvent(window,'resize',this.resizeHandler_); + Dygraph.removeEvent(window,'resize', this.resizeHandler_); this.resizeHandler_ = null; removeRecursive(this.maindiv_); -- 2.7.4