diff --git a/api/router.js b/api/router.js index 46e79998..284558a1 100644 --- a/api/router.js +++ b/api/router.js @@ -218,7 +218,8 @@ module.exports = function($window, mountRedraw) { child.attrs.onclick = null } else { onclick = child.attrs.onclick - href = child.attrs.href + // Easier to build it now to keep it isomorphic. + href = buildPathname(child.attrs.href, child.attrs.params) child.attrs.href = route.prefix + href child.attrs.onclick = function(e) { var result diff --git a/api/tests/test-router.js b/api/tests/test-router.js index 1dd760ef..97bbc4eb 100644 --- a/api/tests/test-router.js +++ b/api/tests/test-router.js @@ -541,6 +541,36 @@ o.spec("route", function() { o(route.set.args[2]).equals(opts) }) + o("passes params on route.Link", function() { + var e = $window.document.createEvent("MouseEvents") + + e.initEvent("click", true, true) + e.button = 0 + $window.location.href = prefix + "/" + + route(root, "/", { + "/" : { + view: lock(function() { + return m(route.Link, { + href: "/test", + params: {key: "value"}, + }) + }) + }, + "/test" : { + view : lock(function() { + return m("div") + }) + } + }) + route.set = o.spy(route.set) + + root.firstChild.dispatchEvent(e) + + o(route.set.callCount).equals(1) + o(route.set.args[0]).equals("/test?key=value") + }) + o("route.Link can render without routes or dom access", function() { $window = browserMock(env) var render = coreRenderer($window) diff --git a/docs/change-log.md b/docs/change-log.md index d11d6ad8..884c0a20 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -24,6 +24,7 @@ - This is unlikely to break people because if you were to do it with `m.render` directly before now, you'd corrupt Mithril's internal representation and internal errors could occur as a result. Now, it just warns you. - For a better debugging experience with `m.route` route resolvers, errors on `onmatch` in the default route are left unhandled and errors in `onmatch` in other routes are logged to the console before redirecting. ([#2536](https://github.com/MithrilJS/mithril.js/pull/2536) [@isiahmeadows](https://github.com/isiahmeadows)) - Bug fix with `m.redraw` where if you removed a root that was previously visited in the current redraw pass, it would lose its place and skip the next root. +- Add `params:` attribute to `m.route.Link`. ([#2537](https://github.com/MithrilJS/mithril.js/pull/2537) [@isiahmeadows](https://github.com/isiahmeadows)) --> diff --git a/docs/route.md b/docs/route.md index 823ca82e..352284bb 100644 --- a/docs/route.md +++ b/docs/route.md @@ -142,6 +142,7 @@ m(m.route.Link, { // first parameter to `m`. selector: "span", options: {replace: true}, + params: {key: "value"}, href: "/test", disabled: false, class: "nav-link", @@ -201,9 +202,10 @@ Do note that this doesn't also disable pointer events for you - you have to do t Argument | Type | Required | Description --------------------- | ------------------------------------ | -------- | --- `attributes.href` | `Object` | Yes | The target route to navigate to. -`attributes.selector` | `String|Object|Function` | No | This sets the tag name to use. Must be a valid selector for [`m`](hyperscript.md) if given, defaults to `"a"`. -`attributes.options` | `Object` | No | This sets the options passed to [`m.route.set`](#mrouteset). -`attributes.disabled` | `Object` | No | This sets the options passed to [`m.route.set`](#mrouteset). +`attributes.selector` | `String|Object|Function` | No | This sets the tag name to use. Must be a valid selector for [`m`](hyperscript.md) if given, defaults to `"a"`. +`attributes.options` | `Object` | No | This sets the options passed to [`m.route.set`](#mrouteset). +`attributes.params` | `Object` | No | This sets the parameters passed to [`m.route.set`](#mrouteset). +`attributes.disabled` | `Object` | No | This disables the link, so clicking on it doesn't route anywhere. `attributes` | `Object` | No | Other attributes to apply to the returned vnode may be passed. `children` | `Array|String|Number|Boolean` | No | Child [vnodes](vnodes.md) for this link. **returns** | `Vnode` | | A [vnode](vnodes.md).