From 1d2ba8b0b0276dce50e0ef592da147c795e29d77 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sun, 1 Jun 2014 22:09:37 -0400 Subject: [PATCH] prevent route change event if only hash changes --- mithril.js | 17 ++++++++++++----- tests/mithril-tests.js | 26 +++++++++++++++++++++----- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/mithril.js b/mithril.js index b3b260fd..2dc953c6 100644 --- a/mithril.js +++ b/mithril.js @@ -53,11 +53,15 @@ Mithril = m = new function app(window) { cached[cacheCount++] = item } if (!intact) { - for (var i = 0; i < data.length; i++) if (cached[i] !== undefined) nodes = nodes.concat(cached[i].nodes) + for (var i = 0; i < data.length; i++) { + if (cached[i] !== undefined) nodes = nodes.concat(cached[i].nodes) + } for (var i = nodes.length, node; node = cached.nodes[i]; i++) { if (node.parentNode !== null && node.parentNode.childNodes.length != nodes.length) node.parentNode.removeChild(node) } - for (var i = cached.nodes.length, node; node = nodes[i]; i++) if (node.parentNode === null) parentElement.appendChild(node) + for (var i = cached.nodes.length, node; node = nodes[i]; i++) { + if (node.parentNode === null) parentElement.appendChild(node) + } if (data.length < cached.length) cached.length = data.length cached.nodes = nodes } @@ -274,20 +278,22 @@ Mithril = m = new function app(window) { m.route = function() { if (arguments.length === 0) return currentRoute else if (arguments.length === 3) { - currentRoute = window.location[m.route.mode].slice(modes[m.route.mode].length) var root = arguments[0], defaultRoute = arguments[1], router = arguments[2] redirect = function(source) { - var path = currentRoute = source.slice(modes[m.route.mode].length) + var path = currentRoute = normalizeRoute(source) if (!routeByValue(root, router, path)) { m.route(defaultRoute, true) } } var listener = m.route.mode == "hash" ? "onhashchange" : "onpopstate" window[listener] = function() { - redirect(window.location[m.route.mode]) + if (currentRoute != normalizeRoute(window.location[m.route.mode])) { + redirect(window.location[m.route.mode]) + } } computePostRedrawHook = scrollToHash window[listener]() + currentRoute = normalizeRoute(window.location[m.route.mode]) } else if (arguments[0].addEventListener) { var element = arguments[0] @@ -319,6 +325,7 @@ Mithril = m = new function app(window) { } m.route.param = function(key) {return routeParams[key]} m.route.mode = "search" + function normalizeRoute(route) {return route.slice(modes[m.route.mode].length)} function routeByValue(root, router, path) { routeParams = {} diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index c3874ad8..288d8d80 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -616,8 +616,8 @@ function testMithril(mock) { var root = mock.document.createElement("div") m.route.mode = "search" m.route(root, "/", { - "/": {controller: function() {}, view: function() {return;}}, - "/test12": {controller: function() {}, view: function() {return;}} + "/": {controller: function() {}, view: function() {}}, + "/test12": {controller: function() {}, view: function() {}} }) mock.performance.$elapse(50) m.route("/test12?a=foo&b=bar") @@ -647,7 +647,7 @@ function testMithril(mock) { m.route.mode = "search" m.route(root, "/", { "/": {controller: function() {}, view: function() {return "bar"}}, - "/test14": {controller: function() {}, view: function() {return "foo" }} + "/test14": {controller: function() {}, view: function() {return "foo"}} }) mock.performance.$elapse(50) m.route("/test14?test&test2=") @@ -661,14 +661,30 @@ function testMithril(mock) { var root = mock.document.createElement("div") m.route.mode = "search" m.route(root, "/", { - "/": {controller: function() {}, view: function() {return;}}, - "/test12": {controller: function() {}, view: function() {return;}} + "/": {controller: function() {}, view: function() {}}, + "/test12": {controller: function() {}, view: function() {}} }) mock.performance.$elapse(50) m.route("/test12", {a: "foo", b: "bar"}) mock.performance.$elapse(50) //teardown return mock.location.search == "?/test12?a=foo&b=bar" && m.route.param("a") == "foo" && m.route.param("b") == "bar" }) + test(function() { + mock.performance.$elapse(50) //setup + mock.location.search = "?" + + var root = mock.document.createElement("div") + var route1, route2 + m.route.mode = "search" + m.route(root, "/", { + "/": {controller: function() {route1 = m.route()}, view: function() {}}, + "/test13": {controller: function() {route2 = m.route()}, view: function() {}} + }) + mock.performance.$elapse(50) + m.route("/test13") + mock.performance.$elapse(50) //teardown + return route1 == "/" && route2 == "/test13" + }) //end m.route //m.prop