Skip to content

Commit

Permalink
Add params: to m.route.Link, fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-claudia committed Sep 30, 2019
1 parent b98ab29 commit ef3c33e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion api/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions api/tests/test-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. ([#????](https://github.com/MithrilJS/mithril.js/pull/????) [@isiahmeadows](https://github.com/isiahmeadows))
-->

Expand Down
8 changes: 5 additions & 3 deletions docs/route.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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<Vnode>|String|Number|Boolean` | No | Child [vnodes](vnodes.md) for this link.
**returns** | `Vnode` | | A [vnode](vnodes.md).
Expand Down

0 comments on commit ef3c33e

Please sign in to comment.