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

Function route

mithril.js:1441–1473  ·  view source on GitHub ↗
(root, defaultRoute, routes)

Source from the content-addressed store, hash-verified

1439 var identity = function(v0) {return v0}
1440 var render1, component, attrs3, currentPath, lastUpdate
1441 var route = function(root, defaultRoute, routes) {
1442 if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined")
1443 function run1() {
1444 if (render1 != null) redrawService0.render(root, render1(Vnode(component, attrs3.key, attrs3)))
1445 }
1446 var redraw2 = function() {
1447 run1()
1448 redraw2 = redrawService0.redraw
1449 }
1450 redrawService0.subscribe(root, run1)
1451 var bail = function(path) {
1452 if (path !== defaultRoute) routeService.setPath(defaultRoute, null, {replace: true})
1453 else throw new Error("Could not resolve default route " + defaultRoute)
1454 }
1455 routeService.defineRoutes(routes, function(payload, params, path) {
1456 var update = lastUpdate = function(routeResolver, comp) {
1457 if (update !== lastUpdate) return
1458 component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
1459 attrs3 = params, currentPath = path, lastUpdate = null
1460 render1 = (routeResolver.render || identity).bind(routeResolver)
1461 redraw2()
1462 }
1463 if (payload.view || typeof payload === "function") update({}, payload)
1464 else {
1465 if (payload.onmatch) {
1466 Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
1467 update(payload, resolved)
1468 }, bail)
1469 }
1470 else update(payload, "div")
1471 }
1472 }, bail)
1473 }
1474 route.set = function(path, data, options) {
1475 if (lastUpdate != null) {
1476 options = options || {}

Callers

nothing calls this directly

Calls 2

redraw2Function · 0.85
updateFunction · 0.50

Tested by

no test coverage detected