add m.route() overload to retrieve current route

This commit is contained in:
Leo Horie 2014-04-28 22:30:40 -04:00
parent 22deb93d04
commit ab434fbc15
64 changed files with 7241 additions and 8 deletions

View file

@ -257,9 +257,11 @@ Mithril = m = new function app(window) {
//routing
var modes = {pathname: "", hash: "#", search: "?"}
var redirect = function() {}, routeParams = {}
var redirect = function() {}, routeParams = {}, currentRoute
m.route = function() {
if (arguments.length == 3) {
if (arguments.length === 0) return currentRoute
else if (arguments.length === 3) {
currentRoute = window.location[m.route.mode].slice(modes[m.route.mode].length)
var root = arguments[0], defaultRoute = arguments[1], router = arguments[2]
redirect = function(source) {
var path = source.slice(modes[m.route.mode].length)
@ -283,16 +285,16 @@ Mithril = m = new function app(window) {
}
}
else if (typeof arguments[0] == "string") {
var route = arguments[0]
currentRoute = arguments[0]
var shouldReplaceHistoryEntry = arguments[1] === true
if (window.history.pushState) {
computePostRedrawHook = function() {
window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + route)
window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute)
scrollToHash()
}
redirect(modes[m.route.mode] + route)
redirect(modes[m.route.mode] + currentRoute)
}
else window.location[m.route.mode] = route
else window.location[m.route.mode] = currentRoute
}
}
m.route.param = function(key) {return routeParams[key]}