Skip to content

Commit 643cefb

Browse files
authored
Partially recast the router API to be a lot more intuitive. (#2469)
* Recast the router API to be a lot more intuitive. Fixes #2387 Fixes #2072 Fixes quite a few issues reported on Gitter. For `m.route.Link`: - More intuitive - More accessible - More ergonomic - It can be disabled - It can be cancelled - It can be changed - Oh, and you can use it isomorphically. For `m.route.prefix` - You can *read* it. - You can write to it, of course. - It's literally just setting a property. For the router itself (and the rest of Mithril): - You can now `require("mithril")` and all its submodules without a DOM at all. There is a catch: you can't instantiate any routes, you can't mount anything, and you can't invoke `m.render` in any capacity. You can only use `m.route.Link`, `m.route.prefix`, hyperscript stuff, and `mithril/stream`, and you can use `m.request` with `background: true` if you use a global XHR polyfill. (You can't use `m.request` without `background: true` except with a DOM to redraw with.) The goal here is to try to get out of the way for simple testing and to defer the inevitable `TypeError`s for the relevant DOM methods to runtime. The factory requires no arguments, and in terms of globals, you can just figure out based on what errors are thrown what globals to define. Their values don't matter - they just need to be set to *something*, even if it's just `null` or `undefined`, before Mithril executes. Had to make quite a few other changes throughout the docs and tests to update them accordingly. Oh, and that massive router overhaul enabled me to do all this. Also, slip in a few drive-by fixes to the mocks so they're a little easier to work with and can accept more URLs. This was required for a few of the tests. * Update changelog + numbers, add forgotten bundle option * Add PR numbers to changelog [skip ci] * Allow continuing to the next match by returning `false`. * Update numbers again
1 parent d244597 commit 643cefb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

threaditjs/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var Header = {
8383
m("a[href='http://threaditjs.com']", "ThreaditJS Home"),
8484
]),
8585
m("h2", [
86-
m("a[href='/']", {oncreate: m.route.link}, "ThreaditJS: Mithril"),
86+
m(m.route.Link, {href: "/"}, "ThreaditJS: Mithril"),
8787
]),
8888
]
8989
}
@@ -102,7 +102,7 @@ var Home = {
102102
threads.map(function(thread) {
103103
return [
104104
m("p", [
105-
m("a", {href: "/thread/" + thread.id, oncreate: m.route.link}, m.trust(T.trimTitle(thread.text))),
105+
m(m.route.Link, {href: "/thread/" + thread.id}, m.trust(T.trimTitle(thread.text))),
106106
]),
107107
m("p.comment_count", thread.comment_count + " comment(s)"),
108108
m("hr"),

todomvc/todomvc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ var Todos = {
115115
state.remaining === 1 ? " item left" : " items left",
116116
]),
117117
m("ul#filters", [
118-
m("li", m("a[href='/']", {oncreate: m.route.link, class: state.showing === "" ? "selected" : ""}, "All")),
119-
m("li", m("a[href='/active']", {oncreate: m.route.link, class: state.showing === "active" ? "selected" : ""}, "Active")),
120-
m("li", m("a[href='/completed']", {oncreate: m.route.link, class: state.showing === "completed" ? "selected" : ""}, "Completed")),
118+
m("li", m(m.route.Link, {href: "/", class: state.showing === "" ? "selected" : ""}, "All")),
119+
m("li", m(m.route.Link, {href: "/active", class: state.showing === "active" ? "selected" : ""}, "Active")),
120+
m("li", m(m.route.Link, {href: "/completed", class: state.showing === "completed" ? "selected" : ""}, "Completed")),
121121
]),
122122
m("button#clear-completed", {onclick: function() {state.dispatch("clear")}}, "Clear completed"),
123123
]) : null,

0 commit comments

Comments
 (0)