Call .destroy() on plugins (fixes #418)
authorDan Vanderkam <danvdk@gmail.com>
Tue, 18 Nov 2014 03:54:09 +0000 (22:54 -0500)
committerDan Vanderkam <danvdk@gmail.com>
Tue, 18 Nov 2014 03:54:09 +0000 (22:54 -0500)
auto_tests/tests/plugins.js
dygraph.js

index 5655585..3605113 100644 (file)
@@ -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);
+};
index 026365b..7dbf29f 100644 (file)
@@ -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_);