From e846335f167eb75f604a39d7b3f5a3c38d544378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Nor=C3=B0fj=C3=B6r=C3=B0?= Date: Fri, 4 Dec 2015 12:53:35 +0000 Subject: [PATCH] fix subcomponent unloading --- mithril.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/mithril.js b/mithril.js index cec9a331..2e553790 100644 --- a/mithril.js +++ b/mithril.js @@ -729,15 +729,19 @@ void (function (global, factory) { // eslint-disable-line var unloaders = [] function updateLists(views, controllers, view, controller) { - if (controller.onunload != null) { - unloaders.push({ - controller: controller, - handler: controller.onunload - }) - } - views.push(view) - controllers.push(controller) + var idx = controllers.push(controller) - 1 + unloaders[idx] = { + controller: controller, + handler: function () { + controllers.splice(controllers.indexOf(controller), 1) + views.splice(views.indexOf(view), 1) + var unload = controller && controller.onunload + if (type.call(unload) === "[object Function]") { + controller.onunload() + } + } + } } var forcing = false @@ -1300,6 +1304,7 @@ void (function (global, factory) { // eslint-disable-line } forEach(unloaders, function (unloader) { + if (unloader.controller == null) return unloader.handler.call(unloader.controller, ev) unloader.controller.onunload = null })