fix and test some edge cases in router

This commit is contained in:
Leo Horie 2016-12-06 19:59:39 -05:00
parent 8bc3c2b4d4
commit c2acdf6f4a
2 changed files with 158 additions and 13 deletions

View file

@ -8,11 +8,11 @@ module.exports = function($window, redrawService) {
var routeService = coreRouter($window)
var identity = function(v) {return v}
var render, component, attrs, currentPath
var routing = false, render, component, attrs, currentPath, resolve
var route = function(root, defaultRoute, routes) {
if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined")
var update = function(routeResolver, comp, params, path) {
component = comp || "div", attrs = params, currentPath = path
component = comp || "div", attrs = params, currentPath = path, resolve = null
render = (routeResolver.render || identity).bind(routeResolver)
run()
}
@ -23,9 +23,15 @@ module.exports = function($window, redrawService) {
if (payload.view) update({}, payload, params, path)
else {
if (payload.onmatch) {
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
update(payload, resolved, params, path)
})
if (resolve != null) update(payload, component, params, path)
else {
resolve = function(resolved) {
update(payload, resolved, params, path)
}
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
if (resolve != null) resolve(resolved)
})
}
}
else update(payload, "div", params, path)
}
@ -34,7 +40,10 @@ module.exports = function($window, redrawService) {
})
redrawService.subscribe(root, run)
}
route.set = routeService.setPath
route.set = function(path, data, options) {
resolve = null
routeService.setPath(path, data, options)
}
route.get = function() {return currentPath}
route.prefix = routeService.setPrefix
route.link = routeService.link