MCPcopy
hub / github.com/MithrilJS/mithril.js / route

Function route

api/router.js:12–44  ·  view source on GitHub ↗
(root, defaultRoute, routes)

Source from the content-addressed store, hash-verified

10 var identity = function(v) {return v}
11 var render, component, attrs, currentPath, lastUpdate
12 var route = function(root, defaultRoute, routes) {
13 if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined")
14 function run() {
15 if (render != null) redrawService.render(root, render(Vnode(component, attrs.key, attrs)))
16 }
17 var redraw = function() {
18 run()
19 redraw = redrawService.redraw
20 }
21 redrawService.subscribe(root, run)
22 var bail = function(path) {
23 if (path !== defaultRoute) routeService.setPath(defaultRoute, null, {replace: true})
24 else throw new Error("Could not resolve default route " + defaultRoute)
25 }
26 routeService.defineRoutes(routes, function(payload, params, path) {
27 var update = lastUpdate = function(routeResolver, comp) {
28 if (update !== lastUpdate) return
29 component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
30 attrs = params, currentPath = path, lastUpdate = null
31 render = (routeResolver.render || identity).bind(routeResolver)
32 redraw()
33 }
34 if (payload.view || typeof payload === "function") update({}, payload)
35 else {
36 if (payload.onmatch) {
37 Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
38 update(payload, resolved)
39 }, bail)
40 }
41 else update(payload, "div")
42 }
43 }, bail)
44 }
45 route.set = function(path, data, options) {
46 if (lastUpdate != null) {
47 options = options || {}

Callers 1

test-router.jsFile · 0.50

Calls 2

redrawFunction · 0.85
updateFunction · 0.50

Tested by

no test coverage detected