From f6a5436269247715901624df016ddf4a84f2b2bb Mon Sep 17 00:00:00 2001 From: Barney Date: Wed, 26 Nov 2014 15:12:15 +0000 Subject: [PATCH] Throw errors when user attempts to query route before it is initialized --- mithril.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mithril.js b/mithril.js index 91348bb4..64c9ef55 100644 --- a/mithril.js +++ b/mithril.js @@ -547,7 +547,9 @@ Mithril = m = new function app(window, undefined) { var redirect = function() {}, routeParams = {}, currentRoute m.route = function() { //m.route() - if (arguments.length === 0) return currentRoute + if (arguments.length === 0) { + return checkRouteInit() && currentRoute + } else if (arguments.length === 3 && type.call(arguments[1]) == sStr) { var root = arguments[0], defaultRoute = arguments[1], router = arguments[2] redirect = function(source) { @@ -592,7 +594,9 @@ Mithril = m = new function app(window, undefined) { else window.location[m.route.mode] = currentRoute } } - m.route.param = function(key) {return routeParams[key]} + m.route.param = function(key) { + return checkRouteInit() && routeParams[key] + } m.route.mode = "search" function normalizeRoute(route) {return route.slice(modes[m.route.mode].length)} function routeByValue(root, router, path) { @@ -655,6 +659,9 @@ Mithril = m = new function app(window, undefined) { function decodeSpace(string) { return decodeURIComponent(string.replace(/\+/g, " ")) } + function checkRouteInit(){ + return currentRoute == null || throw "Attempted to query m.route, but m.route was not initialized"; + } function reset(root) { var cacheKey = getCellCacheKey(root) clear(root.childNodes, cellCache[cacheKey])