Deduplicate m.route and m.redraw logic (#2453)
- Remove appropriate route change subcriptions when a root is removed via `m.mount(root, null)`. - Don't pollute `onpopstate` and friends - use standard event listeners instead. - Simplify and streamline subscriptions, in preparation of adding a `remove` parameter to `m.mount`. - Change the redraw internals to redraw immediately, with ability to cancel via returning a sentinel. - Change `"bleeding-edge"` for `m.version` in `next` to instead just be the latest `m.version`. (If you're using `next`, you should know what you're in for.) - Update tests to be aware of these changes. (Some were failing for subtle reasons.) - Drive-by: remove some uses of `string.charAt(n)` and use `string[n]` instead.
This commit is contained in:
parent
6c562d2b9b
commit
90bcff0fa7
18 changed files with 397 additions and 192 deletions
|
|
@ -11,6 +11,10 @@ o.spec("Router.defineRoutes", function() {
|
|||
o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() {
|
||||
var $window, router, onRouteChange, onFail
|
||||
|
||||
function defineRoutes(routes, defaultRoute) {
|
||||
router.defineRoutes(routes, onRouteChange, onFail, defaultRoute, function() {})
|
||||
}
|
||||
|
||||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
router = new Router($window)
|
||||
|
|
@ -21,7 +25,10 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("calls onRouteChange on init", function(done) {
|
||||
$window.location.href = prefix + "/a"
|
||||
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail)
|
||||
var subscribe = o.spy()
|
||||
|
||||
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail, null, subscribe)
|
||||
o(subscribe.callCount).equals(1)
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -32,7 +39,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("resolves to route", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -45,7 +52,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("resolves to route w/ escaped unicode", function(done) {
|
||||
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6"
|
||||
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/ö": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -58,7 +65,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("resolves to route w/ unicode", function(done) {
|
||||
$window.location.href = prefix + "/ö?ö=ö"
|
||||
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/ö": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -75,7 +82,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
router = new Router($window)
|
||||
router.prefix = prefix
|
||||
|
||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -88,7 +95,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles parameterized route", function(done) {
|
||||
$window.location.href = prefix + "/test/x"
|
||||
router.defineRoutes({"/test/:a": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test/:a": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -101,7 +108,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles multi-parameterized route", function(done) {
|
||||
$window.location.href = prefix + "/test/x/y"
|
||||
router.defineRoutes({"/test/:a/:b": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test/:a/:b": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -114,7 +121,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles rest parameterized route", function(done) {
|
||||
$window.location.href = prefix + "/test/x/y"
|
||||
router.defineRoutes({"/test/:a...": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test/:a...": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -127,7 +134,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles route with search", function(done) {
|
||||
$window.location.href = prefix + "/test?a=b&c=d"
|
||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -140,7 +147,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("calls reject", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/other": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onFail.callCount).equals(1)
|
||||
|
|
@ -152,7 +159,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles out of order routes", function(done) {
|
||||
$window.location.href = prefix + "/z/y/x"
|
||||
router.defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -164,7 +171,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles reverse out of order routes", function(done) {
|
||||
$window.location.href = prefix + "/z/y/x"
|
||||
router.defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -180,7 +187,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
routes["/:a..."] = {data: 2}
|
||||
|
||||
$window.location.href = prefix + "/z/y/x"
|
||||
router.defineRoutes(routes, onRouteChange, onFail)
|
||||
defineRoutes(routes)
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -196,7 +203,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
routes["/z/y/x"] = {data: 1}
|
||||
|
||||
$window.location.href = prefix + "/z/y/x"
|
||||
router.defineRoutes(routes, onRouteChange, onFail)
|
||||
defineRoutes(routes)
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -211,7 +218,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
routes["/:a..."] = {data: 2}
|
||||
|
||||
$window.location.href = prefix + "/z/y/x"
|
||||
router.defineRoutes(routes, onRouteChange, onFail)
|
||||
defineRoutes(routes)
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -226,7 +233,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
routes["/z/y/x"] = {data: 12}
|
||||
|
||||
$window.location.href = prefix + "/z/y/x"
|
||||
router.defineRoutes(routes, onRouteChange, onFail)
|
||||
defineRoutes(routes)
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
@ -238,7 +245,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
|
||||
o("handles non-ascii routes", function(done) {
|
||||
$window.location.href = prefix + "/ö"
|
||||
router.defineRoutes({"/ö": "aaa"}, onRouteChange, onFail)
|
||||
defineRoutes({"/ö": "aaa"})
|
||||
|
||||
callAsync(function() {
|
||||
o(onRouteChange.callCount).equals(1)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ o.spec("Router.getPath", function() {
|
|||
o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() {
|
||||
var $window, router, onRouteChange, onFail
|
||||
|
||||
function defineRoutes(routes, defaultRoute) {
|
||||
router.defineRoutes(routes, onRouteChange, onFail, defaultRoute, function() {})
|
||||
}
|
||||
|
||||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
router = new Router($window)
|
||||
|
|
@ -20,25 +24,25 @@ o.spec("Router.getPath", function() {
|
|||
|
||||
o("gets route", function() {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}})
|
||||
|
||||
o(router.getPath()).equals("/test")
|
||||
})
|
||||
o("gets route w/ params", function() {
|
||||
$window.location.href = prefix + "/other/x/y/z?c=d#e=f"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}})
|
||||
|
||||
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
||||
})
|
||||
o("gets route w/ escaped unicode", function() {
|
||||
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6"
|
||||
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}})
|
||||
|
||||
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
|
||||
})
|
||||
o("gets route w/ unicode", function() {
|
||||
$window.location.href = prefix + "/ö?ö=ö#ö=ö"
|
||||
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}})
|
||||
|
||||
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
|
||||
})
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@ o.spec("Router.setPath", function() {
|
|||
o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() {
|
||||
var $window, router, onRouteChange, onFail
|
||||
|
||||
function defineRoutes(routes, defaultRoute) {
|
||||
router.defineRoutes(routes, onRouteChange, onFail, defaultRoute, function() {})
|
||||
}
|
||||
|
||||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
router = new Router($window)
|
||||
|
|
@ -21,7 +25,7 @@ o.spec("Router.setPath", function() {
|
|||
|
||||
o("setPath calls onRouteChange asynchronously", function(done) {
|
||||
$window.location.href = prefix + "/a"
|
||||
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/a": {data: 1}, "/b": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/b")
|
||||
|
|
@ -35,7 +39,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("setPath calls onFail asynchronously", function(done) {
|
||||
$window.location.href = prefix + "/a"
|
||||
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/a": {data: 1}, "/b": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/c")
|
||||
|
|
@ -49,7 +53,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("sets route via API", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/other/x/y/z?c=d#e=f")
|
||||
|
|
@ -61,7 +65,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("sets route w/ escaped unicode", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6")
|
||||
|
|
@ -73,7 +77,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("sets route w/ unicode", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/ö?ö=ö#ö=ö")
|
||||
|
|
@ -90,7 +94,7 @@ o.spec("Router.setPath", function() {
|
|||
router = new Router($window)
|
||||
router.prefix = prefix
|
||||
|
||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/other/x/y/z?c=d#e=f")
|
||||
|
|
@ -102,7 +106,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("sets route via pushState/onpopstate", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
$window.history.pushState(null, null, prefix + "/other/x/y/z?c=d#e=f")
|
||||
|
|
@ -115,7 +119,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("sets parameterized route", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/other/:a/:b", {a: "x", b: "y/z", c: "d", e: "f"})
|
||||
|
|
@ -127,7 +131,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("replace:true works", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/other", null, {replace: true})
|
||||
|
|
@ -140,7 +144,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("replace:false works", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/other", null, {replace: false})
|
||||
|
|
@ -155,7 +159,7 @@ o.spec("Router.setPath", function() {
|
|||
})
|
||||
o("state works", function(done) {
|
||||
$window.location.href = prefix + "/test"
|
||||
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
||||
defineRoutes({"/test": {data: 1}, "/other": {data: 2}})
|
||||
|
||||
callAsync(function() {
|
||||
router.setPath("/other", null, {state: {a: 1}})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue