diff --git a/api/router.js b/api/router.js index 04b5d091..be13d8a4 100644 --- a/api/router.js +++ b/api/router.js @@ -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 diff --git a/api/tests/test-router.js b/api/tests/test-router.js index 7c93438a..6ac64be6 100644 --- a/api/tests/test-router.js +++ b/api/tests/test-router.js @@ -456,13 +456,15 @@ o.spec("route", function() { o("onmatch can redirect to another route", function(done) { var redirected = false - + var render = o.spy() + $window.location.href = prefix + "/a" route(root, "/a", { "/a" : { onmatch: function() { route.set("/b") - } + }, + render: render }, "/b" : { view: function(vnode){ @@ -472,6 +474,7 @@ o.spec("route", function() { }) callAsync(function() { + o(render.callCount).equals(0) o(redirected).equals(true) done() @@ -480,13 +483,15 @@ o.spec("route", function() { o("onmatch can redirect to another route that has RouteResolver w/ only onmatch", function(done) { var redirected = false + var render = o.spy() $window.location.href = prefix + "/a" route(root, "/a", { "/a" : { onmatch: function() { route.set("/b") - } + }, + render: render }, "/b" : { onmatch: function() { @@ -497,6 +502,7 @@ o.spec("route", function() { }) callAsync(function() { + o(render.callCount).equals(0) o(redirected).equals(true) done() @@ -505,13 +511,15 @@ o.spec("route", function() { o("onmatch can redirect to another route that has RouteResolver w/ only render", function(done) { var redirected = false + var render = o.spy() $window.location.href = prefix + "/a" route(root, "/a", { "/a" : { onmatch: function() { route.set("/b") - } + }, + render: render }, "/b" : { render: function(vnode){ @@ -521,12 +529,101 @@ o.spec("route", function() { }) callAsync(function() { + o(render.callCount).equals(0) o(redirected).equals(true) done() }) }) + o("onmatch can redirect to a non-existent route that defaults to a RouteResolver w/ onmatch", function(done) { + var redirected = false + var render = o.spy() + + $window.location.href = prefix + "/a" + route(root, "/b", { + "/a" : { + onmatch: function() { + route.set("/c") + }, + render: render + }, + "/b" : { + onmatch: function(vnode){ + redirected = true + return {view: function() {}} + } + } + }) + + callAsync(function() { + callAsync(function() { + o(render.callCount).equals(0) + o(redirected).equals(true) + + done() + }) + }) + }) + + o("onmatch can redirect to a non-existent route that defaults to a RouteResolver w/ render", function(done) { + var redirected = false + var render = o.spy() + + $window.location.href = prefix + "/a" + route(root, "/b", { + "/a" : { + onmatch: function() { + route.set("/c") + }, + render: render + }, + "/b" : { + render: function(vnode){ + redirected = true + } + } + }) + + callAsync(function() { + callAsync(function() { + o(render.callCount).equals(0) + o(redirected).equals(true) + + done() + }) + }) + }) + + o("onmatch can redirect to a non-existent route that defaults to a component", function(done) { + var redirected = false + var render = o.spy() + + $window.location.href = prefix + "/a" + route(root, "/b", { + "/a" : { + onmatch: function() { + route.set("/c") + }, + render: render + }, + "/b" : { + view: function(vnode){ + redirected = true + } + } + }) + + callAsync(function() { + callAsync(function() { + o(render.callCount).equals(0) + o(redirected).equals(true) + + done() + }) + }) + }) + o("the previous view redraws while onmatch resolution is pending (#1268)", function(done) { var view = o.spy() var onmatch = o.spy(function() { @@ -609,9 +706,7 @@ o.spec("route", function() { }) }) - o("routing with RouteResolver works more than once", function(done, timeout) { - timeout(200) - + o("routing with RouteResolver works more than once", function(done) { $window.location.href = prefix + "/a" route(root, '/a', { '/a': { @@ -676,6 +771,47 @@ o.spec("route", function() { }) }) + o("calling route.set invalidates pending onmatch resolution", function(done) { + var rendered = false + var resolved + $window.location.href = prefix + "/a" + route(root, "/a", { + "/a": { + onmatch: function() { + return new Promise(function(resolve) { + callAsync(function() { + callAsync(function() { + resolve({view: function() {rendered = true}}) + }) + }) + }) + }, + render: function(vnode) { + rendered = true + resolved = "a" + } + }, + "/b": { + view: function() { + resolved = "b" + } + } + }) + + route.set("/b") + + callAsync(function() { + o(rendered).equals(false) + o(resolved).equals("b") + + callAsync(function() { + o(rendered).equals(false) + o(resolved).equals("b") + done() + }) + }) + }) + o("route changes activate onbeforeremove", function(done) { var spy = o.spy()