Merge pull request #1461 from pygy/rewrite-router-cleanup
[rewrite] router cleanup
This commit is contained in:
commit
678a8b13ae
1 changed files with 10 additions and 16 deletions
|
|
@ -8,37 +8,31 @@ 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 resolver, component, attrs, currentPath, resolve
|
var render, component, attrs, currentPath
|
||||||
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) {
|
||||||
resolver = routeResolver, component = comp, attrs = params, currentPath = path, resolve = null
|
component = comp || "div", attrs = params, currentPath = path
|
||||||
resolver.render = routeResolver.render || identity
|
render = (routeResolver.render || identity).bind(routeResolver)
|
||||||
render()
|
run()
|
||||||
}
|
}
|
||||||
var render = function() {
|
var run = function() {
|
||||||
if (resolver != null) redrawService.render(root, resolver.render(Vnode(component || "div", attrs.key, attrs)))
|
if (render != null) redrawService.render(root, render(Vnode(component, attrs.key, attrs)))
|
||||||
}
|
}
|
||||||
routeService.defineRoutes(routes, function(payload, params, path) {
|
routeService.defineRoutes(routes, function(payload, params, path) {
|
||||||
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)
|
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
|
||||||
else {
|
update(payload, resolved, params, path)
|
||||||
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)
|
else update(payload, "div", params, path)
|
||||||
}
|
}
|
||||||
}, function() {
|
}, function() {
|
||||||
routeService.setPath(defaultRoute)
|
routeService.setPath(defaultRoute)
|
||||||
})
|
})
|
||||||
redrawService.subscribe(root, render)
|
redrawService.subscribe(root, run)
|
||||||
}
|
}
|
||||||
route.set = routeService.setPath
|
route.set = routeService.setPath
|
||||||
route.get = function() {return currentPath}
|
route.get = function() {return currentPath}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue