From: Dan Vanderkam Date: Tue, 18 Nov 2014 03:54:09 +0000 (-0500) Subject: Call .destroy() on plugins (fixes #418) X-Git-Tag: v1.1.0~22 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=92c5f414ef8132225bd6677d092335e8b38e5a06;p=dygraphs.git Call .destroy() on plugins (fixes #418) --- 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_);