Add params: to m.route.Link, fix docs (#2537)

This commit is contained in:
Isiah Meadows 2019-09-30 17:16:24 -04:00 committed by GitHub
parent b98ab29efd
commit 3fa1630f91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 4 deletions

View file

@ -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

View file

@ -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)

View file

@ -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))
-->

View file

@ -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<Vnode>|String|Number|Boolean` | No | Child [vnodes](vnodes.md) for this link.
**returns** | `Vnode` | | A [vnode](vnodes.md).