resolve hook in router
This commit is contained in:
parent
b3d784d841
commit
a6c56ff6b9
10 changed files with 599 additions and 319 deletions
|
|
@ -1,14 +1,27 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
var coreRenderer = require("../render/render")
|
var Node = require("../render/node")
|
||||||
var coreRouter = require("../router/router")
|
var coreRouter = require("../router/router")
|
||||||
var autoredraw = require("../api/autoredraw")
|
var autoredraw = require("../api/autoredraw")
|
||||||
|
|
||||||
module.exports = function($window, renderer, pubsub) {
|
module.exports = function($window, renderer, pubsub) {
|
||||||
var router = coreRouter($window)
|
var router = coreRouter($window)
|
||||||
var route = function(root, defaultRoute, routes) {
|
var route = function(root, defaultRoute, routes) {
|
||||||
var replay = router.defineRoutes(routes, function(component, args) {
|
var current = {route: null, component: null}
|
||||||
renderer.render(root, {tag: component, attrs: args})
|
var replay = router.defineRoutes(routes, function(payload, args, path, route) {
|
||||||
|
if (typeof payload.view !== "function") {
|
||||||
|
if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode}
|
||||||
|
var render = function(component) {
|
||||||
|
current.route = route, current.component = component
|
||||||
|
renderer.render(root, payload.render(Node(component, null, args, undefined, undefined, undefined)))
|
||||||
|
}
|
||||||
|
if (typeof payload.resolve !== "function") payload.resolve = function() {render(current.component)}
|
||||||
|
if (route !== current.route) payload.resolve(render, args, path, route)
|
||||||
|
else render(current.component)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
renderer.render(root, Node(payload, null, args, undefined, undefined, undefined))
|
||||||
|
}
|
||||||
}, function() {
|
}, function() {
|
||||||
router.setPath(defaultRoute)
|
router.setPath(defaultRoute)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -11,175 +11,287 @@ var apiPubSub = require("../../api/pubsub")
|
||||||
var apiRouter = require("../../api/router")
|
var apiRouter = require("../../api/router")
|
||||||
|
|
||||||
o.spec("route", function() {
|
o.spec("route", function() {
|
||||||
var FRAME_BUDGET = Math.floor(1000 / 60)
|
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
|
||||||
var $window, root, redraw, route
|
o.spec("using prefix `" + prefix + "`", function() {
|
||||||
|
var FRAME_BUDGET = Math.floor(1000 / 60)
|
||||||
|
var $window, root, redraw, route
|
||||||
|
|
||||||
o.beforeEach(function() {
|
o.beforeEach(function() {
|
||||||
$window = {}
|
$window = {}
|
||||||
|
|
||||||
var dom = domMock()
|
var dom = domMock()
|
||||||
for (var key in dom) $window[key] = dom[key]
|
for (var key in dom) $window[key] = dom[key]
|
||||||
|
|
||||||
var loc = pushStateMock()
|
var loc = pushStateMock()
|
||||||
for (var key in loc) $window[key] = loc[key]
|
for (var key in loc) $window[key] = loc[key]
|
||||||
|
|
||||||
root = $window.document.body
|
root = $window.document.body
|
||||||
|
|
||||||
redraw = apiPubSub()
|
redraw = apiPubSub()
|
||||||
route = apiRouter($window, coreRenderer($window), redraw)
|
route = apiRouter($window, coreRenderer($window), redraw)
|
||||||
})
|
route.prefix(prefix)
|
||||||
|
})
|
||||||
|
|
||||||
o("renders into `root`", function(done) {
|
o("renders into `root`", function(done) {
|
||||||
route(root, "/", {
|
$window.location.href = prefix + "/"
|
||||||
"/" : {
|
route(root, "/", {
|
||||||
view: function() {
|
"/" : {
|
||||||
return m("div")
|
view: function() {
|
||||||
}
|
return m("div")
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
})
|
||||||
|
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
o(root.firstChild.nodeName).equals("DIV")
|
o(root.firstChild.nodeName).equals("DIV")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
o("redraws when render function is executed", function(done) {
|
||||||
|
var onupdate = o.spy()
|
||||||
|
var oninit = o.spy()
|
||||||
|
|
||||||
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
|
"/" : {
|
||||||
|
view: function() {
|
||||||
|
return m("div", {
|
||||||
|
oninit: oninit,
|
||||||
|
onupdate: onupdate
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
o(oninit.callCount).equals(1)
|
||||||
|
|
||||||
|
redraw.publish()
|
||||||
|
|
||||||
|
// Wrapped to give time for the rate-limited redraw to fire
|
||||||
|
setTimeout(function() {
|
||||||
|
o(onupdate.callCount).equals(1)
|
||||||
|
|
||||||
|
done()
|
||||||
|
}, FRAME_BUDGET)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
o("redraws on events", function(done) {
|
||||||
|
var onupdate = o.spy()
|
||||||
|
var oninit = o.spy()
|
||||||
|
var onclick = o.spy()
|
||||||
|
var e = $window.document.createEvent("MouseEvents")
|
||||||
|
|
||||||
|
e.initEvent("click", true, true)
|
||||||
|
|
||||||
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
|
"/" : {
|
||||||
|
view: function() {
|
||||||
|
return m("div", {
|
||||||
|
oninit: oninit,
|
||||||
|
onupdate: onupdate,
|
||||||
|
onclick: onclick,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
root.firstChild.dispatchEvent(e)
|
||||||
|
|
||||||
|
o(oninit.callCount).equals(1)
|
||||||
|
|
||||||
|
o(onclick.callCount).equals(1)
|
||||||
|
o(onclick.this).equals(root.firstChild)
|
||||||
|
o(onclick.args[0].type).equals("click")
|
||||||
|
o(onclick.args[0].target).equals(root.firstChild)
|
||||||
|
|
||||||
|
// Wrapped to give time for the rate-limited redraw to fire
|
||||||
|
setTimeout(function() {
|
||||||
|
o(onupdate.callCount).equals(1)
|
||||||
|
|
||||||
|
done()
|
||||||
|
}, FRAME_BUDGET)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
o("event handlers can skip redraw", function(done) {
|
||||||
|
var onupdate = o.spy()
|
||||||
|
var oninit = o.spy()
|
||||||
|
var onclick = o.spy()
|
||||||
|
var e = $window.document.createEvent("MouseEvents")
|
||||||
|
|
||||||
|
e.initEvent("click", true, true)
|
||||||
|
|
||||||
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
|
"/" : {
|
||||||
|
view: function() {
|
||||||
|
return m("div", {
|
||||||
|
oninit: oninit,
|
||||||
|
onupdate: onupdate,
|
||||||
|
onclick: function(e) {
|
||||||
|
e.redraw = false
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
root.firstChild.dispatchEvent(e)
|
||||||
|
|
||||||
|
o(oninit.callCount).equals(1)
|
||||||
|
|
||||||
|
// Wrapped to ensure no redraw fired
|
||||||
|
setTimeout(function() {
|
||||||
|
o(onupdate.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
}, FRAME_BUDGET)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
o("changes location on route.link", function(done) {
|
||||||
|
var e = $window.document.createEvent("MouseEvents")
|
||||||
|
|
||||||
|
e.initEvent("click", true, true)
|
||||||
|
|
||||||
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
|
"/" : {
|
||||||
|
view: function() {
|
||||||
|
return m("a", {
|
||||||
|
href: "/test",
|
||||||
|
oncreate: route.link
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/test" : {
|
||||||
|
view : function() {
|
||||||
|
return m("div")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
var slash = prefix[0] === "/" ? "" : "/"
|
||||||
|
|
||||||
|
o($window.location.href).equals("http://localhost" + slash + (prefix ? prefix + "/" : ""))
|
||||||
|
|
||||||
|
root.firstChild.dispatchEvent(e)
|
||||||
|
|
||||||
|
o($window.location.href).equals("http://localhost" + slash + (prefix ? prefix + "/" : "") + "test")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
done()
|
o("accepts object as payload", function(done) {
|
||||||
})
|
var Component = {
|
||||||
})
|
view: function() {
|
||||||
|
return m("div")
|
||||||
o("redraws when render function is executed", function(done) {
|
}
|
||||||
var onupdate = o.spy()
|
|
||||||
var oninit = o.spy()
|
|
||||||
|
|
||||||
route(root, "/", {
|
|
||||||
"/" : {
|
|
||||||
view: function() {
|
|
||||||
return m("div", {
|
|
||||||
oninit: oninit,
|
|
||||||
onupdate: onupdate
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
callAsync(function() {
|
"/" : {
|
||||||
o(oninit.callCount).equals(1)
|
resolve: function(resolve) {resolve(Component)},
|
||||||
|
render: function(vnode) {return vnode},
|
||||||
redraw.publish()
|
},
|
||||||
|
})
|
||||||
// Wrapped to give time for the rate-limited redraw to fire
|
|
||||||
setTimeout(function() {
|
callAsync(function() {
|
||||||
o(onupdate.callCount).equals(1)
|
o(root.firstChild.nodeName).equals("DIV")
|
||||||
|
|
||||||
done()
|
done()
|
||||||
}, FRAME_BUDGET)
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
o("accepts object without `render` method as payload", function(done) {
|
||||||
o("redraws on events", function(done) {
|
var Component = {
|
||||||
var onupdate = o.spy()
|
view: function() {
|
||||||
var oninit = o.spy()
|
return m("div")
|
||||||
var onclick = o.spy()
|
}
|
||||||
var e = $window.document.createEvent("MouseEvents")
|
|
||||||
|
|
||||||
e.initEvent("click", true, true)
|
|
||||||
|
|
||||||
route(root, "/", {
|
|
||||||
"/" : {
|
|
||||||
view: function() {
|
|
||||||
return m("div", {
|
|
||||||
oninit: oninit,
|
|
||||||
onupdate: onupdate,
|
|
||||||
onclick: onclick,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
|
"/" : {
|
||||||
|
resolve: function(resolve) {resolve(Component)},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
o(root.firstChild.nodeName).equals("DIV")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
o("accepts object without `resolve` method as payload", function(done) {
|
||||||
|
var Component = {
|
||||||
|
view: function() {
|
||||||
|
return m("div")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$window.location.href = prefix + "/"
|
||||||
|
route(root, "/", {
|
||||||
|
"/" : {
|
||||||
|
render: function() {return m(Component)},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
o(root.firstChild.nodeName).equals("DIV")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
callAsync(function() {
|
o("calls resolve and render correct number of times", function(done) {
|
||||||
root.firstChild.dispatchEvent(e)
|
var resolveCount = 0
|
||||||
|
var renderCount = 0
|
||||||
o(oninit.callCount).equals(1)
|
var Component = {
|
||||||
|
view: function() {
|
||||||
o(onclick.callCount).equals(1)
|
return m("div")
|
||||||
o(onclick.this).equals(root.firstChild)
|
}
|
||||||
o(onclick.args[0].type).equals("click")
|
}
|
||||||
o(onclick.args[0].target).equals(root.firstChild)
|
|
||||||
|
$window.location.href = prefix + "/"
|
||||||
// Wrapped to give time for the rate-limited redraw to fire
|
route(root, "/", {
|
||||||
setTimeout(function() {
|
"/" : {
|
||||||
o(onupdate.callCount).equals(1)
|
resolve: function(resolve) {
|
||||||
|
resolveCount++
|
||||||
done()
|
resolve(Component)
|
||||||
}, FRAME_BUDGET)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
o("event handlers can skip redraw", function(done) {
|
|
||||||
var onupdate = o.spy()
|
|
||||||
var oninit = o.spy()
|
|
||||||
var onclick = o.spy()
|
|
||||||
var e = $window.document.createEvent("MouseEvents")
|
|
||||||
|
|
||||||
e.initEvent("click", true, true)
|
|
||||||
|
|
||||||
route(root, "/", {
|
|
||||||
"/" : {
|
|
||||||
view: function() {
|
|
||||||
return m("div", {
|
|
||||||
oninit: oninit,
|
|
||||||
onupdate: onupdate,
|
|
||||||
onclick: function(e) {
|
|
||||||
e.redraw = false
|
|
||||||
},
|
},
|
||||||
})
|
render: function(vnode) {
|
||||||
}
|
renderCount++
|
||||||
}
|
return vnode
|
||||||
})
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
root.firstChild.dispatchEvent(e)
|
o(resolveCount).equals(1)
|
||||||
|
o(renderCount).equals(1)
|
||||||
|
|
||||||
|
redraw.publish()
|
||||||
|
|
||||||
o(oninit.callCount).equals(1)
|
setTimeout(function() {
|
||||||
|
o(resolveCount).equals(1)
|
||||||
// Wrapped to ensure no redraw fired
|
o(renderCount).equals(2)
|
||||||
setTimeout(function() {
|
|
||||||
o(onupdate.callCount).equals(0)
|
done()
|
||||||
|
}, FRAME_BUDGET)
|
||||||
done()
|
})
|
||||||
}, FRAME_BUDGET)
|
})
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
o("changes location on route.link", function(done) {
|
|
||||||
var e = $window.document.createEvent("MouseEvents")
|
|
||||||
|
|
||||||
e.initEvent("click", true, true)
|
|
||||||
|
|
||||||
route.prefix("?")
|
|
||||||
|
|
||||||
route(root, "/", {
|
|
||||||
"/" : {
|
|
||||||
view: function() {
|
|
||||||
return m("a", {
|
|
||||||
href: "/test",
|
|
||||||
oncreate: route.link
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/test" : {
|
|
||||||
view : function() {
|
|
||||||
return m("div")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
callAsync(function() {
|
|
||||||
o($window.location.href).equals("http://localhost/?/")
|
|
||||||
|
|
||||||
root.firstChild.dispatchEvent(e)
|
|
||||||
|
|
||||||
o($window.location.href).equals("http://localhost/?/test")
|
|
||||||
|
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
65
mithril.js
65
mithril.js
|
|
@ -471,10 +471,15 @@ var renderService = function($window) {
|
||||||
vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode))
|
vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode))
|
||||||
updateLifecycle(vnode.tag, vnode, hooks, recycling)
|
updateLifecycle(vnode.tag, vnode, hooks, recycling)
|
||||||
if (vnode.instance != null) {
|
if (vnode.instance != null) {
|
||||||
updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling, ns)
|
if (old.instance == null) insertNode(parent, createNode(vnode.instance, hooks, ns), nextSibling)
|
||||||
|
else updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling, ns)
|
||||||
vnode.dom = vnode.instance.dom
|
vnode.dom = vnode.instance.dom
|
||||||
vnode.domSize = vnode.instance.domSize
|
vnode.domSize = vnode.instance.domSize
|
||||||
}
|
}
|
||||||
|
else if (old.instance != null) {
|
||||||
|
removeNode(parent, old.instance, null, false)
|
||||||
|
vnode.dom = vnode.domSize = undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function isRecyclable(old, vnodes) {
|
function isRecyclable(old, vnodes) {
|
||||||
if (old.pool != null && Math.abs(old.pool.length - vnodes.length) <= Math.abs(old.length - vnodes.length)) {
|
if (old.pool != null && Math.abs(old.pool.length - vnodes.length) <= Math.abs(old.length - vnodes.length)) {
|
||||||
|
|
@ -557,7 +562,7 @@ var renderService = function($window) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (vnode.dom.parentNode != null) parent.removeChild(vnode.dom)
|
if (vnode.dom.parentNode != null) parent.removeChild(vnode.dom)
|
||||||
if (context != null && vnode.domSize == null && !hasIntegrationMethods(vnode.attrs) && !(typeof vnode.tag !== "string" && hasIntegrationMethods(vnode.tag))) { //TODO test custom elements
|
if (context != null && vnode.domSize == null && !hasIntegrationMethods(vnode.attrs) && typeof vnode.tag === "string") { //TODO test custom elements
|
||||||
if (!context.pool) context.pool = [vnode]
|
if (!context.pool) context.pool = [vnode]
|
||||||
else context.pool.push(vnode)
|
else context.pool.push(vnode)
|
||||||
}
|
}
|
||||||
|
|
@ -836,7 +841,6 @@ var requestService = function($window) {
|
||||||
args.data[args.callbackKey || "callback"] = callbackName
|
args.data[args.callbackKey || "callback"] = callbackName
|
||||||
script.src = assemble(args.url, args.data)
|
script.src = assemble(args.url, args.data)
|
||||||
$window.document.documentElement.appendChild(script)
|
$window.document.documentElement.appendChild(script)
|
||||||
|
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
function interpolate(url, data) {
|
function interpolate(url, data) {
|
||||||
|
|
@ -958,7 +962,7 @@ var coreRouter = function($window) {
|
||||||
if (supportsPushState) {
|
if (supportsPushState) {
|
||||||
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
|
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
|
||||||
else $window.history.pushState(null, null, prefix + path)
|
else $window.history.pushState(null, null, prefix + path)
|
||||||
callAsync($window.onpopstate)
|
$window.onpopstate()
|
||||||
}
|
}
|
||||||
else $window.location.href = prefix + path
|
else $window.location.href = prefix + path
|
||||||
}
|
}
|
||||||
|
|
@ -966,25 +970,28 @@ var coreRouter = function($window) {
|
||||||
if (supportsPushState) $window.onpopstate = resolveRoute
|
if (supportsPushState) $window.onpopstate = resolveRoute
|
||||||
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
||||||
resolveRoute()
|
resolveRoute()
|
||||||
|
|
||||||
function resolveRoute() {
|
function resolveRoute() {
|
||||||
var path = getPath()
|
var path = getPath()
|
||||||
var params = {}
|
var params = {}
|
||||||
var pathname = parsePath(path, params, params)
|
var pathname = parsePath(path, params, params)
|
||||||
for (var route in routes) {
|
callAsync(function() {
|
||||||
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
|
for (var route in routes) {
|
||||||
if (matcher.test(pathname)) {
|
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
|
||||||
pathname.replace(matcher, function() {
|
if (matcher.test(pathname)) {
|
||||||
var keys = route.match(/:[^\/]+/g) || []
|
pathname.replace(matcher, function() {
|
||||||
var values = [].slice.call(arguments, 1, -2)
|
var keys = route.match(/:[^\/]+/g) || []
|
||||||
for (var i = 0; i < keys.length; i++) {
|
var values = [].slice.call(arguments, 1, -2)
|
||||||
params[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
for (var i = 0; i < keys.length; i++) {
|
||||||
}
|
params[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
||||||
resolve(routes[route], params, path, route)
|
}
|
||||||
})
|
resolve(routes[route], params, path, route)
|
||||||
return
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
reject(path, params)
|
||||||
reject(path, params)
|
})
|
||||||
}
|
}
|
||||||
return resolveRoute
|
return resolveRoute
|
||||||
}
|
}
|
||||||
|
|
@ -992,6 +999,7 @@ var coreRouter = function($window) {
|
||||||
vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
|
vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
|
||||||
vnode.dom.onclick = function(e) {
|
vnode.dom.onclick = function(e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
e.redraw = false
|
||||||
setPath(vnode.attrs.href, undefined, undefined)
|
setPath(vnode.attrs.href, undefined, undefined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1033,8 +1041,21 @@ var autoredraw = function(root, renderer, pubsub, callback) {
|
||||||
m.route = function($window, renderer, pubsub) {
|
m.route = function($window, renderer, pubsub) {
|
||||||
var router = coreRouter($window)
|
var router = coreRouter($window)
|
||||||
var route = function(root, defaultRoute, routes) {
|
var route = function(root, defaultRoute, routes) {
|
||||||
var replay = router.defineRoutes(routes, function(component, args) {
|
var current = {route: null, component: null}
|
||||||
renderer.render(root, {tag: component, attrs: args})
|
var replay = router.defineRoutes(routes, function(payload, args, path, route) {
|
||||||
|
if (typeof payload.view !== "function") {
|
||||||
|
if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode}
|
||||||
|
var render = function(component) {
|
||||||
|
current.route = route, current.component = component
|
||||||
|
renderer.render(root, payload.render(Node(component, null, args, undefined, undefined, undefined)))
|
||||||
|
}
|
||||||
|
if (typeof payload.resolve !== "function") payload.resolve = function() {render(current.component)}
|
||||||
|
if (route !== current.route) payload.resolve(render, args, path, route)
|
||||||
|
else render(current.component)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
renderer.render(root, Node(payload, null, args, undefined, undefined, undefined))
|
||||||
|
}
|
||||||
}, function() {
|
}, function() {
|
||||||
router.setPath(defaultRoute)
|
router.setPath(defaultRoute)
|
||||||
})
|
})
|
||||||
|
|
@ -1042,8 +1063,8 @@ m.route = function($window, renderer, pubsub) {
|
||||||
}
|
}
|
||||||
route.link = router.link
|
route.link = router.link
|
||||||
route.prefix = router.setPrefix
|
route.prefix = router.setPrefix
|
||||||
route.setPath = router.setPath
|
route.set = router.setPath
|
||||||
route.getPath = router.getPath
|
route.get = router.getPath
|
||||||
return route
|
return route
|
||||||
}(window, renderService, redrawService)
|
}(window, renderService, redrawService)
|
||||||
m.mount = function(renderer, pubsub) {
|
m.mount = function(renderer, pubsub) {
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,13 @@ o.spec("ospec", function() {
|
||||||
o.spec("sync", function() {
|
o.spec("sync", function() {
|
||||||
var a = 0, b = 0
|
var a = 0, b = 0
|
||||||
|
|
||||||
o.before(function test() {a = 1})
|
o.before(function() {a = 1})
|
||||||
o.after(function test() {a = 0})
|
o.after(function() {a = 0})
|
||||||
|
|
||||||
o.beforeEach(function test() {b = 1})
|
o.beforeEach(function() {b = 1})
|
||||||
o.afterEach(function test() {b = 0})
|
o.afterEach(function() {b = 0})
|
||||||
|
|
||||||
o("assertions", function test() {
|
o("assertions", function() {
|
||||||
var spy = o.spy()
|
var spy = o.spy()
|
||||||
spy(a)
|
spy(a)
|
||||||
|
|
||||||
|
|
@ -53,33 +53,33 @@ o.spec("ospec", function() {
|
||||||
o.spec("async", function() {
|
o.spec("async", function() {
|
||||||
var a = 0, b = 0
|
var a = 0, b = 0
|
||||||
|
|
||||||
o.before(function test(done) {
|
o.before(function(done) {
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
a = 1
|
a = 1
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
o.after(function test(done) {
|
o.after(function(done) {
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
a = 0
|
a = 0
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o.beforeEach(function test(done) {
|
o.beforeEach(function(done) {
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
b = 1
|
b = 1
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
o.afterEach(function test(done) {
|
o.afterEach(function(done) {
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
b = 0
|
b = 0
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("async hooks", function test(done) {
|
o("async hooks", function(done) {
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
var spy = o.spy()
|
var spy = o.spy()
|
||||||
spy(a)
|
spy(a)
|
||||||
|
|
|
||||||
|
|
@ -337,7 +337,7 @@ module.exports = function($window) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (vnode.dom.parentNode != null) parent.removeChild(vnode.dom)
|
if (vnode.dom.parentNode != null) parent.removeChild(vnode.dom)
|
||||||
if (context != null && vnode.domSize == null && !hasIntegrationMethods(vnode.attrs) && !(typeof vnode.tag !== "string" && hasIntegrationMethods(vnode.tag))) { //TODO test custom elements
|
if (context != null && vnode.domSize == null && !hasIntegrationMethods(vnode.attrs) && typeof vnode.tag === "string") { //TODO test custom elements
|
||||||
if (!context.pool) context.pool = [vnode]
|
if (!context.pool) context.pool = [vnode]
|
||||||
else context.pool.push(vnode)
|
else context.pool.push(vnode)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -771,6 +771,7 @@ o.spec("updateNodes", function() {
|
||||||
render(root, temp)
|
render(root, temp)
|
||||||
render(root, updated)
|
render(root, updated)
|
||||||
|
|
||||||
|
o(root.childNodes.length).equals(1)
|
||||||
o(vnodes[0].dom).equals(updated[0].dom)
|
o(vnodes[0].dom).equals(updated[0].dom)
|
||||||
o(updated[0].dom.nodeName).equals("DIV")
|
o(updated[0].dom.nodeName).equals("DIV")
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,6 @@ module.exports = function($window) {
|
||||||
args.data[args.callbackKey || "callback"] = callbackName
|
args.data[args.callbackKey || "callback"] = callbackName
|
||||||
script.src = assemble(args.url, args.data)
|
script.src = assemble(args.url, args.data)
|
||||||
$window.document.documentElement.appendChild(script)
|
$window.document.documentElement.appendChild(script)
|
||||||
|
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ module.exports = function($window) {
|
||||||
if (supportsPushState) {
|
if (supportsPushState) {
|
||||||
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
|
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
|
||||||
else $window.history.pushState(null, null, prefix + path)
|
else $window.history.pushState(null, null, prefix + path)
|
||||||
callAsync($window.onpopstate)
|
$window.onpopstate()
|
||||||
}
|
}
|
||||||
else $window.location.href = prefix + path
|
else $window.location.href = prefix + path
|
||||||
}
|
}
|
||||||
|
|
@ -70,29 +70,31 @@ module.exports = function($window) {
|
||||||
if (supportsPushState) $window.onpopstate = resolveRoute
|
if (supportsPushState) $window.onpopstate = resolveRoute
|
||||||
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
||||||
resolveRoute()
|
resolveRoute()
|
||||||
|
|
||||||
function resolveRoute() {
|
function resolveRoute() {
|
||||||
var path = getPath()
|
var path = getPath()
|
||||||
var params = {}
|
var params = {}
|
||||||
var pathname = parsePath(path, params, params)
|
var pathname = parsePath(path, params, params)
|
||||||
|
|
||||||
for (var route in routes) {
|
callAsync(function() {
|
||||||
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
|
for (var route in routes) {
|
||||||
|
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
|
||||||
|
|
||||||
if (matcher.test(pathname)) {
|
if (matcher.test(pathname)) {
|
||||||
pathname.replace(matcher, function() {
|
pathname.replace(matcher, function() {
|
||||||
var keys = route.match(/:[^\/]+/g) || []
|
var keys = route.match(/:[^\/]+/g) || []
|
||||||
var values = [].slice.call(arguments, 1, -2)
|
var values = [].slice.call(arguments, 1, -2)
|
||||||
for (var i = 0; i < keys.length; i++) {
|
for (var i = 0; i < keys.length; i++) {
|
||||||
params[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
params[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
||||||
}
|
}
|
||||||
resolve(routes[route], params, path, route)
|
resolve(routes[route], params, path, route)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
reject(path, params)
|
reject(path, params)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return resolveRoute
|
return resolveRoute
|
||||||
}
|
}
|
||||||
|
|
@ -101,6 +103,7 @@ module.exports = function($window) {
|
||||||
vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
|
vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
|
||||||
vnode.dom.onclick = function(e) {
|
vnode.dom.onclick = function(e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
e.redraw = false
|
||||||
setPath(vnode.attrs.href, undefined, undefined)
|
setPath(vnode.attrs.href, undefined, undefined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
var o = require("../../ospec/ospec")
|
var o = require("../../ospec/ospec")
|
||||||
|
var callAsync = require("../../test-utils/callAsync")
|
||||||
var pushStateMock = require("../../test-utils/pushStateMock")
|
var pushStateMock = require("../../test-utils/pushStateMock")
|
||||||
var Router = require("../../router/router")
|
var Router = require("../../router/router")
|
||||||
|
|
||||||
|
|
@ -17,41 +18,57 @@ o.spec("Router.defineRoutes", function() {
|
||||||
onFail = o.spy()
|
onFail = o.spy()
|
||||||
})
|
})
|
||||||
|
|
||||||
o("calls onRouteChange on init", function() {
|
o("calls onRouteChange on init", function(done) {
|
||||||
$window.location.href = prefix + "/a"
|
$window.location.href = prefix + "/a"
|
||||||
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("resolves to route", function() {
|
o("resolves to route", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("resolves to route w/ escaped unicode", function() {
|
o("resolves to route w/ escaped unicode", function(done) {
|
||||||
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6"
|
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6"
|
||||||
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 2}, {"ö": "ö"}, "/ö?ö=ö#ö=ö", "/ö"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 2}, {"ö": "ö"}, "/ö?ö=ö#ö=ö", "/ö"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("resolves to route w/ unicode", function() {
|
o("resolves to route w/ unicode", function(done) {
|
||||||
$window.location.href = prefix + "/ö?ö=ö#ö=ö"
|
$window.location.href = prefix + "/ö?ö=ö#ö=ö"
|
||||||
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 2}, {"ö": "ö"}, "/ö?ö=ö#ö=ö", "/ö"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 2}, {"ö": "ö"}, "/ö?ö=ö#ö=ö", "/ö"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("resolves to route on fallback mode", function() {
|
o("resolves to route on fallback mode", function(done) {
|
||||||
$window.location.href = "file://" + prefix + "/test"
|
$window.location.href = "file://" + prefix + "/test"
|
||||||
|
|
||||||
router = new Router($window)
|
router = new Router($window)
|
||||||
|
|
@ -59,98 +76,142 @@ o.spec("Router.defineRoutes", function() {
|
||||||
|
|
||||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles parameterized route", function() {
|
o("handles parameterized route", function(done) {
|
||||||
$window.location.href = prefix + "/test/x"
|
$window.location.href = prefix + "/test/x"
|
||||||
router.defineRoutes({"/test/:a": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test/:a": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x"}, "/test/x", "/test/:a"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x"}, "/test/x", "/test/:a"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles multi-parameterized route", function() {
|
o("handles multi-parameterized route", function(done) {
|
||||||
$window.location.href = prefix + "/test/x/y"
|
$window.location.href = prefix + "/test/x/y"
|
||||||
router.defineRoutes({"/test/:a/:b": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test/:a/:b": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x", b: "y"}, "/test/x/y", "/test/:a/:b"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x", b: "y"}, "/test/x/y", "/test/:a/:b"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles rest parameterized route", function() {
|
o("handles rest parameterized route", function(done) {
|
||||||
$window.location.href = prefix + "/test/x/y"
|
$window.location.href = prefix + "/test/x/y"
|
||||||
router.defineRoutes({"/test/:a...": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test/:a...": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x/y"}, "/test/x/y", "/test/:a..."])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x/y"}, "/test/x/y", "/test/:a..."])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles route with search", function() {
|
o("handles route with search", function(done) {
|
||||||
$window.location.href = prefix + "/test?a=b&c=d"
|
$window.location.href = prefix + "/test?a=b&c=d"
|
||||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test?a=b&c=d", "/test"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test?a=b&c=d", "/test"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles route with hash", function() {
|
o("handles route with hash", function(done) {
|
||||||
$window.location.href = prefix + "/test#a=b&c=d"
|
$window.location.href = prefix + "/test#a=b&c=d"
|
||||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test#a=b&c=d", "/test"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test#a=b&c=d", "/test"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles route with search and hash", function() {
|
o("handles route with search and hash", function(done) {
|
||||||
$window.location.href = prefix + "/test?a=b#c=d"
|
$window.location.href = prefix + "/test?a=b#c=d"
|
||||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test?a=b#c=d", "/test"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test?a=b#c=d", "/test"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("calls reject", function() {
|
o("calls reject", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onFail.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onFail.args).deepEquals(["/test", {}])
|
o(onFail.callCount).equals(1)
|
||||||
|
o(onFail.args).deepEquals(["/test", {}])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("calls reject w/ search and hash", function() {
|
o("calls reject w/ search and hash", function(done) {
|
||||||
$window.location.href = prefix + "/test?a=b#c=d"
|
$window.location.href = prefix + "/test?a=b#c=d"
|
||||||
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onFail.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onFail.args).deepEquals(["/test?a=b#c=d", {a: "b", c: "d"}])
|
o(onFail.callCount).equals(1)
|
||||||
|
o(onFail.args).deepEquals(["/test?a=b#c=d", {a: "b", c: "d"}])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles out of order routes", function() {
|
o("handles out of order routes", function(done) {
|
||||||
$window.location.href = prefix + "/z/y/x"
|
$window.location.href = prefix + "/z/y/x"
|
||||||
router.defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles reverse out of order routes", function() {
|
o("handles reverse out of order routes", function(done) {
|
||||||
$window.location.href = prefix + "/z/y/x"
|
$window.location.href = prefix + "/z/y/x"
|
||||||
router.defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}}, onRouteChange, onFail)
|
router.defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles dynamically added out of order routes", function() {
|
o("handles dynamically added out of order routes", function(done) {
|
||||||
var routes = {}
|
var routes = {}
|
||||||
routes["/z/y/x"] = {data: 1}
|
routes["/z/y/x"] = {data: 1}
|
||||||
routes["/:a..."] = {data: 2}
|
routes["/:a..."] = {data: 2}
|
||||||
|
|
@ -158,11 +219,15 @@ o.spec("Router.defineRoutes", function() {
|
||||||
$window.location.href = prefix + "/z/y/x"
|
$window.location.href = prefix + "/z/y/x"
|
||||||
router.defineRoutes(routes, onRouteChange, onFail)
|
router.defineRoutes(routes, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles reversed dynamically added out of order routes", function() {
|
o("handles reversed dynamically added out of order routes", function(done) {
|
||||||
var routes = {}
|
var routes = {}
|
||||||
routes["/:a..."] = {data: 2}
|
routes["/:a..."] = {data: 2}
|
||||||
routes["/z/y/x"] = {data: 1}
|
routes["/z/y/x"] = {data: 1}
|
||||||
|
|
@ -170,47 +235,67 @@ o.spec("Router.defineRoutes", function() {
|
||||||
$window.location.href = prefix + "/z/y/x"
|
$window.location.href = prefix + "/z/y/x"
|
||||||
router.defineRoutes(routes, onRouteChange, onFail)
|
router.defineRoutes(routes, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles mixed out of order routes", function() {
|
o("handles mixed out of order routes", function(done) {
|
||||||
var routes = {"/z/y/x": {data: 1}}
|
var routes = {"/z/y/x": {data: 1}}
|
||||||
routes["/:a..."] = {data: 2}
|
routes["/:a..."] = {data: 2}
|
||||||
|
|
||||||
$window.location.href = prefix + "/z/y/x"
|
$window.location.href = prefix + "/z/y/x"
|
||||||
router.defineRoutes(routes, onRouteChange, onFail)
|
router.defineRoutes(routes, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles reverse mixed out of order routes", function() {
|
o("handles reverse mixed out of order routes", function(done) {
|
||||||
var routes = {"/:a...": {data: 2}}
|
var routes = {"/:a...": {data: 2}}
|
||||||
routes["/z/y/x"] = {data: 12}
|
routes["/z/y/x"] = {data: 12}
|
||||||
|
|
||||||
$window.location.href = prefix + "/z/y/x"
|
$window.location.href = prefix + "/z/y/x"
|
||||||
router.defineRoutes(routes, onRouteChange, onFail)
|
router.defineRoutes(routes, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("handles non-ascii routes", function() {
|
o("handles non-ascii routes", function(done) {
|
||||||
$window.location.href = prefix + "/ö"
|
$window.location.href = prefix + "/ö"
|
||||||
router.defineRoutes({"/ö": "aaa"}, onRouteChange, onFail)
|
router.defineRoutes({"/ö": "aaa"}, onRouteChange, onFail)
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
callAsync(function() {
|
||||||
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("replays", function() {
|
o("replays", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
var replay = router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
var replay = router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||||
replay()
|
replay()
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(2)
|
callAsync(function() {
|
||||||
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
|
o(onRouteChange.callCount).equals(2)
|
||||||
o(onFail.callCount).equals(0)
|
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -21,90 +21,136 @@ o.spec("Router.setPath", function() {
|
||||||
o("setPath calls onRouteChange asynchronously", function(done) {
|
o("setPath calls onRouteChange asynchronously", function(done) {
|
||||||
$window.location.href = prefix + "/a"
|
$window.location.href = prefix + "/a"
|
||||||
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/b")
|
|
||||||
|
|
||||||
o(onRouteChange.callCount).equals(1)
|
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
o(onRouteChange.callCount).equals(2)
|
router.setPath("/b")
|
||||||
done()
|
|
||||||
|
o(onRouteChange.callCount).equals(1)
|
||||||
|
callAsync(function() {
|
||||||
|
o(onRouteChange.callCount).equals(2)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
o("setPath calls onFail asynchronously", function(done) {
|
o("setPath calls onFail asynchronously", function(done) {
|
||||||
$window.location.href = prefix + "/a"
|
$window.location.href = prefix + "/a"
|
||||||
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/c")
|
|
||||||
|
|
||||||
o(onFail.callCount).equals(0)
|
|
||||||
callAsync(function() {
|
callAsync(function() {
|
||||||
o(onFail.callCount).equals(1)
|
router.setPath("/c")
|
||||||
|
|
||||||
|
o(onFail.callCount).equals(0)
|
||||||
|
callAsync(function() {
|
||||||
|
o(onFail.callCount).equals(1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
o("sets route via API", function(done) {
|
||||||
|
$window.location.href = prefix + "/test"
|
||||||
|
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||||
|
|
||||||
|
callAsync(function() {
|
||||||
|
router.setPath("/other/x/y/z?c=d#e=f")
|
||||||
|
|
||||||
|
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
||||||
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
o("sets route via API", function() {
|
o("sets route w/ escaped unicode", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
|
||||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
|
||||||
router.setPath("/other/x/y/z?c=d#e=f")
|
|
||||||
|
|
||||||
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
|
||||||
})
|
|
||||||
o("sets route w/ escaped unicode", function() {
|
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6")
|
|
||||||
|
|
||||||
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
|
callAsync(function() {
|
||||||
|
router.setPath("/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6")
|
||||||
|
|
||||||
|
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
o("sets route w/ unicode", function() {
|
o("sets route w/ unicode", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/ö?ö=ö#ö=ö")
|
|
||||||
|
|
||||||
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
|
callAsync(function() {
|
||||||
|
router.setPath("/ö?ö=ö#ö=ö")
|
||||||
|
|
||||||
|
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
o("sets route on fallback mode", function() {
|
o("sets route on fallback mode", function(done) {
|
||||||
$window.location.href = "file://" + prefix + "/test"
|
$window.location.href = "file://" + prefix + "/test"
|
||||||
|
|
||||||
router = new Router($window)
|
router = new Router($window)
|
||||||
router.setPrefix(prefix)
|
router.setPrefix(prefix)
|
||||||
|
|
||||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/other/x/y/z?c=d#e=f")
|
|
||||||
|
|
||||||
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
callAsync(function() {
|
||||||
|
router.setPath("/other/x/y/z?c=d#e=f")
|
||||||
|
|
||||||
|
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
o("sets route via pushState/onpopstate", function() {
|
o("sets route via pushState/onpopstate", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||||
$window.history.pushState(null, null, prefix + "/other/x/y/z?c=d#e=f")
|
|
||||||
$window.onpopstate()
|
|
||||||
|
|
||||||
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
callAsync(function() {
|
||||||
|
$window.history.pushState(null, null, prefix + "/other/x/y/z?c=d#e=f")
|
||||||
|
$window.onpopstate()
|
||||||
|
|
||||||
|
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
o("sets parameterized route", function() {
|
o("sets parameterized route", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/other/:a/:b", {a: "x", b: "y/z", c: "d", e: "f"})
|
|
||||||
|
|
||||||
o(router.getPath()).equals("/other/x/y/z?c=d&e=f")
|
callAsync(function() {
|
||||||
|
router.setPath("/other/:a/:b", {a: "x", b: "y/z", c: "d", e: "f"})
|
||||||
|
|
||||||
|
o(router.getPath()).equals("/other/x/y/z?c=d&e=f")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
o("replace:true works", function() {
|
o("replace:true works", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/other", null, {replace: true})
|
|
||||||
$window.history.back()
|
|
||||||
|
|
||||||
o($window.location.href).equals("http://localhost/")
|
callAsync(function() {
|
||||||
|
router.setPath("/other", null, {replace: true})
|
||||||
|
$window.history.back()
|
||||||
|
|
||||||
|
o($window.location.href).equals("http://localhost/")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
o("replace:false works", function() {
|
o("replace:false works", function(done) {
|
||||||
$window.location.href = prefix + "/test"
|
$window.location.href = prefix + "/test"
|
||||||
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
|
||||||
router.setPath("/other", null, {replace: false})
|
|
||||||
$window.history.back()
|
|
||||||
|
|
||||||
var slash = prefix[0] === "/" ? "" : "/"
|
callAsync(function() {
|
||||||
|
router.setPath("/other", null, {replace: false})
|
||||||
|
$window.history.back()
|
||||||
|
|
||||||
o($window.location.href).equals("http://localhost" + slash + (prefix ? prefix + "/" : "") + "test")
|
var slash = prefix[0] === "/" ? "" : "/"
|
||||||
|
|
||||||
|
o($window.location.href).equals("http://localhost" + slash + (prefix ? prefix + "/" : "") + "test")
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue