From e9e2a45caacfe76bc7817b4355bb522ad34381e3 Mon Sep 17 00:00:00 2001 From: Jared Jacobs Date: Wed, 29 Jun 2016 22:42:45 -0700 Subject: [PATCH] Fixing m.route() after a canceled navigation (#1126) --- mithril.js | 17 ++++++++++++----- test/mithril.route.js | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/mithril.js b/mithril.js index 28030022..4e972b89 100644 --- a/mithril.js +++ b/mithril.js @@ -1435,8 +1435,13 @@ removeRootElement(root, index) } return controllers[index] - } else if (component == null) { - removeRootElement(root, index) + } else { + if (component == null) { + removeRootElement(root, index) + } + if (previousRoute) { + currentRoute = previousRoute + } } } @@ -1570,7 +1575,7 @@ var modes = {pathname: "", hash: "#", search: "?"} var redirect = noop var isDefaultRoute = false - var routeParams, currentRoute + var routeParams, currentRoute, previousRoute m.route = function (root, arg1, arg2, vdom) { // eslint-disable-line // m.route() @@ -1623,7 +1628,7 @@ } // m.route(route, params, shouldReplaceHistoryEntry) if (isString(root)) { - var oldRoute = currentRoute + previousRoute = currentRoute currentRoute = root var args = arg1 || {} @@ -1659,7 +1664,7 @@ var replaceHistory = (arguments.length === 3 ? arg2 : arg1) === true || - oldRoute === root + previousRoute === root if (global.history.pushState) { var method = replaceHistory ? "replaceState" : "pushState" @@ -1681,6 +1686,8 @@ $location[m.route.mode] = currentRoute redirect(modes[m.route.mode] + currentRoute) } + + previousRoute = null } } diff --git a/test/mithril.route.js b/test/mithril.route.js index 76ac5c09..11c529a6 100644 --- a/test/mithril.route.js +++ b/test/mithril.route.js @@ -127,6 +127,7 @@ describe("m.route()", function () { route("/b") expect(spy).to.not.have.been.called + expect(m.route()).to.equal("/a") }) dit("skips route change if subcomponent ctrl.onunload calls preventDefault", function (root) { // eslint-disable-line @@ -157,6 +158,7 @@ describe("m.route()", function () { route("/b") expect(spy).to.not.have.been.called + expect(m.route()).to.equal("/a") }) dit("initializes a component's constructor on route change", function (root) { // eslint-disable-line