From 83f497e1296c9a8806a3e0962ae0e4626a840a6d Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Thu, 20 Mar 2014 11:18:40 -0400 Subject: [PATCH] reset m.route.param correctly on route change --- archive/v0.1.1/mithril-tests.js | 19 ++++++++++++++++++- docs/change-log.md | 16 +++++++++++++++- mithril.js | 2 +- tests/mithril-tests.js | 17 +++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/archive/v0.1.1/mithril-tests.js b/archive/v0.1.1/mithril-tests.js index bddb9236..bb34ed35 100644 --- a/archive/v0.1.1/mithril-tests.js +++ b/archive/v0.1.1/mithril-tests.js @@ -258,6 +258,7 @@ new function(window) { m.route.param = function(key) {return routeParams[key]} m.route.mode = "search" function routeByValue(root, router, path) { + routeParams = {} for (var route in router) { if (route == path) return !void m.module(root, router[route]) @@ -266,7 +267,6 @@ new function(window) { return !void path.replace(matcher, function() { var keys = route.match(/:[^\/]+/g) var values = [].slice.call(arguments, 1, -2) - routeParams = {} for (var i = 0; i < keys.length; i++) routeParams[keys[i].slice(1)] = values[i] m.module(root, router[route]) }) @@ -656,6 +656,23 @@ function testMithril(mock) { }) return mock.location.search == "?/test4/foo" && root.childNodes[0].nodeValue === "foo" }) + test(function() { + mock.performance.$elapse(50) + + var module = {controller: function() {}, view: function() {return m.route.param("test")}} + + var root = mock.document.createElement("div") + m.route.mode = "search" + m.route(root, "/test5/foo", { + "/": module, + "/test5/:test": module + }) + var paramValueBefore = m.route.param("test") + m.route("/") + var paramValueAfter = m.route.param("test") + + return mock.location.search == "?/" && paramValueBefore === "foo" && paramValueAfter === undefined + }) //m.prop test(function() { diff --git a/docs/change-log.md b/docs/change-log.md index 362395d7..b7db69f4 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -2,8 +2,22 @@ [v0.1.1](/archive/v0.1.1) - maintenance +### Bug Fixes: + +- `m.route.param` now resets on route change correctly [#15](https://github.com/lhorie/mithril.js/issues/15) + ### Breaking changes: -- changed default value for `xhr.withCredentials` from `true` to `false` for `m.request`, since public APIs are more common than auth-walled ones +- changed default value for `xhr.withCredentials` from `true` to `false` for `m.request`, since public APIs are more common than auth-walled ones. [#14](https://github.com/lhorie/mithril.js/issues/14) + + In order to configure this flag, the following configuration should be used: + + ```javascript + var privateAPI = function(xhr) {xhr.withCredentials = true}; + + m.request({method: "GET", url: "/foo", config: privateAPI}); + ``` + +--- [v0.1](/archive/v0.1) - Initial release \ No newline at end of file diff --git a/mithril.js b/mithril.js index 9eb476c5..12f6fdc5 100644 --- a/mithril.js +++ b/mithril.js @@ -258,6 +258,7 @@ new function(window) { m.route.param = function(key) {return routeParams[key]} m.route.mode = "search" function routeByValue(root, router, path) { + routeParams = {} for (var route in router) { if (route == path) return !void m.module(root, router[route]) @@ -266,7 +267,6 @@ new function(window) { return !void path.replace(matcher, function() { var keys = route.match(/:[^\/]+/g) var values = [].slice.call(arguments, 1, -2) - routeParams = {} for (var i = 0; i < keys.length; i++) routeParams[keys[i].slice(1)] = values[i] m.module(root, router[route]) }) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index c1a7faed..8625eff9 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -153,6 +153,23 @@ function testMithril(mock) { }) return mock.location.search == "?/test4/foo" && root.childNodes[0].nodeValue === "foo" }) + test(function() { + mock.performance.$elapse(50) + + var module = {controller: function() {}, view: function() {return m.route.param("test")}} + + var root = mock.document.createElement("div") + m.route.mode = "search" + m.route(root, "/test5/foo", { + "/": module, + "/test5/:test": module + }) + var paramValueBefore = m.route.param("test") + m.route("/") + var paramValueAfter = m.route.param("test") + + return mock.location.search == "?/" && paramValueBefore === "foo" && paramValueAfter === undefined + }) //m.prop test(function() {