From 6ef1f8ae2e2cda14ceadcac7581e8d2df5ea72de Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Tue, 25 Nov 2014 19:36:25 -0500 Subject: [PATCH] don't duplicate route args when redirecting #352 --- mithril.js | 19 ++++++++++++------- tests/mithril-tests.js | 26 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/mithril.js b/mithril.js index 254d7526..8a17d522 100644 --- a/mithril.js +++ b/mithril.js @@ -1,4 +1,4 @@ -Mithril = m = new function app(window, undefined) { +var m = (function app(window, undefined) { var OBJECT = "[object Object]", ARRAY = "[object Array]", STRING = "[object String]", FUNCTION = "function"; var type = {}.toString; var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g, attrParser = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/; @@ -581,11 +581,16 @@ Mithril = m = new function app(window, undefined) { element.removeEventListener("click", routeUnobtrusive); element.addEventListener("click", routeUnobtrusive) } - //m.route(route) + //m.route(route, params) else if (type.call(arguments[0]) == STRING) { currentRoute = arguments[0]; - var querystring = arguments[1] != null && type.call(arguments[1]) == OBJECT ? buildQueryString(arguments[1]) : null; - if (querystring) currentRoute += (currentRoute.indexOf("?") === -1 ? "?" : "&") + querystring; + var args = arguments[1] || {} + var queryIndex = currentRoute.indexOf("?") + var params = queryIndex > -1 ? parseQueryString(currentRoute.slice(queryIndex + 1)) : {} + for (var i in args) params[i] = args[i] + var querystring = buildQueryString(params) + var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute + if (querystring) currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring; var shouldReplaceHistoryEntry = (arguments.length == 3 ? arguments[2] : arguments[1]) === true; @@ -655,7 +660,7 @@ Mithril = m = new function app(window, undefined) { var pairs = str.split("&"), params = {}; for (var i = 0; i < pairs.length; i++) { var pair = pairs[i].split("="); - params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : (pair.length === 1 ? true : "") + params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : "" } return params } @@ -972,7 +977,7 @@ Mithril = m = new function app(window, undefined) { m.deps.factory = app; return m -}(typeof window != "undefined" ? window : {}); +})(typeof window != "undefined" ? window : {}); if (typeof module != "undefined" && module !== null && module.exports) module.exports = m; -if (typeof define == "function" && define.amd) define(function() {return m}); +else if (typeof define == "function" && define.amd) define(function() {return m}); diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 6b3212d5..4160d24b 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -1063,7 +1063,7 @@ function testMithril(mock) { mock.requestAnimationFrame.$resolve() m.route("/test14?test&test2=") mock.requestAnimationFrame.$resolve() //teardown - return mock.location.search == "?/test14?test&test2=" && m.route.param("test") === true && m.route.param("test2") === "" + return mock.location.search == "?/test14?test=&test2=" && m.route.param("test") === "" && m.route.param("test2") === "" }) test(function() { mock.requestAnimationFrame.$resolve() //setup @@ -1597,6 +1597,30 @@ function testMithril(mock) { return root.childNodes[0].nodeValue == "b" }) + test(function() { + mock.requestAnimationFrame.$resolve() + mock.location.search = "?" + + var root = mock.document.createElement("div") + + var a = {} + a.controller = function() { + m.route("/b?foo=1", {foo: 2}) + } + a.view = function() {return "a"} + + var b = {} + b.controller = function() {} + b.view = function() {return "b"} + + m.route(root, "/", { + "/": a, + "/b": b, + }) + mock.requestAnimationFrame.$resolve() + + return mock.location.search == "?/b?foo=2" + }) //end m.route //m.prop