Merge pull request #1471 from pygy/rewrite-router-cleanup-the-revenge

[api/router] simplify the route finalization logic
This commit is contained in:
Leo Horie 2016-12-07 17:09:03 -05:00 committed by GitHub
commit df7d2c51cc

View file

@ -8,11 +8,11 @@ module.exports = function($window, redrawService) {
var routeService = coreRouter($window) var routeService = coreRouter($window)
var identity = function(v) {return v} var identity = function(v) {return v}
var routing = false, render, component, attrs, currentPath, resolve var render, component, attrs, currentPath, updatePending = false
var route = function(root, defaultRoute, routes) { 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") 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) { var update = function(routeResolver, comp, params, path) {
component = comp != null && typeof comp.view === "function" ? comp : "div", attrs = params, currentPath = path, resolve = null component = comp != null && typeof comp.view === "function" ? comp : "div", attrs = params, currentPath = path, updatePending = false
render = (routeResolver.render || identity).bind(routeResolver) render = (routeResolver.render || identity).bind(routeResolver)
run() run()
} }
@ -26,15 +26,10 @@ module.exports = function($window, redrawService) {
if (payload.view) update({}, payload, params, path) if (payload.view) update({}, payload, params, path)
else { else {
if (payload.onmatch) { if (payload.onmatch) {
if (resolve != null) update(payload, component, params, path) updatePending = true
else { Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
resolve = function(resolved) { if (updatePending) update(payload, resolved, params, path)
update(payload, resolved, params, path) }, bail)
}
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
if (resolve != null) resolve(resolved)
}, bail)
}
} }
else update(payload, "div", params, path) else update(payload, "div", params, path)
} }
@ -42,8 +37,8 @@ module.exports = function($window, redrawService) {
redrawService.subscribe(root, run) redrawService.subscribe(root, run)
} }
route.set = function(path, data, options) { route.set = function(path, data, options) {
if (resolve != null) options = {replace: true} if (updatePending) options = {replace: true}
resolve = null updatePending = false
routeService.setPath(path, data, options) routeService.setPath(path, data, options)
} }
route.get = function() {return currentPath} route.get = function() {return currentPath}