Add parameter names to m.route
This commit is contained in:
parent
293df33858
commit
b28045aa60
1 changed files with 16 additions and 19 deletions
35
mithril.js
35
mithril.js
|
|
@ -701,18 +701,17 @@ var m = (function app(window, undefined) {
|
||||||
//routing
|
//routing
|
||||||
var modes = {pathname: "", hash: "#", search: "?"};
|
var modes = {pathname: "", hash: "#", search: "?"};
|
||||||
var redirect = noop, routeParams, currentRoute, isDefaultRoute = false;
|
var redirect = noop, routeParams, currentRoute, isDefaultRoute = false;
|
||||||
m.route = function() {
|
m.route = function(root, arg1, arg2, vdom) {
|
||||||
//m.route()
|
//m.route()
|
||||||
if (arguments.length === 0) return currentRoute;
|
if (arguments.length === 0) return currentRoute;
|
||||||
//m.route(el, defaultRoute, routes)
|
//m.route(el, defaultRoute, routes)
|
||||||
else if (arguments.length === 3 && type.call(arguments[1]) === STRING) {
|
else if (arguments.length === 3 && type.call(arg1) === STRING) {
|
||||||
var root = arguments[0], defaultRoute = arguments[1], router = arguments[2];
|
|
||||||
redirect = function(source) {
|
redirect = function(source) {
|
||||||
var path = currentRoute = normalizeRoute(source);
|
var path = currentRoute = normalizeRoute(source);
|
||||||
if (!routeByValue(root, router, path)) {
|
if (!routeByValue(root, arg2, path)) {
|
||||||
if (isDefaultRoute) throw new Error("Ensure the default route matches one of the routes defined in m.route");
|
if (isDefaultRoute) throw new Error("Ensure the default route matches one of the routes defined in m.route");
|
||||||
isDefaultRoute = true;
|
isDefaultRoute = true;
|
||||||
m.route(defaultRoute, true);
|
m.route(arg1, true);
|
||||||
isDefaultRoute = false;
|
isDefaultRoute = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -728,32 +727,30 @@ var m = (function app(window, undefined) {
|
||||||
window[listener]();
|
window[listener]();
|
||||||
}
|
}
|
||||||
//config: m.route
|
//config: m.route
|
||||||
else if (arguments[0].addEventListener || arguments[0].attachEvent) {
|
else if (root.addEventListener || root.attachEvent) {
|
||||||
var element = arguments[0];
|
root.href = (m.route.mode !== 'pathname' ? $location.pathname : '') + modes[m.route.mode] + vdom.attrs.href;
|
||||||
var vdom = arguments[3];
|
if (root.addEventListener) {
|
||||||
element.href = (m.route.mode !== 'pathname' ? $location.pathname : '') + modes[m.route.mode] + vdom.attrs.href;
|
root.removeEventListener("click", routeUnobtrusive);
|
||||||
if (element.addEventListener) {
|
root.addEventListener("click", routeUnobtrusive);
|
||||||
element.removeEventListener("click", routeUnobtrusive);
|
|
||||||
element.addEventListener("click", routeUnobtrusive);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
element.detachEvent("onclick", routeUnobtrusive);
|
root.detachEvent("onclick", routeUnobtrusive);
|
||||||
element.attachEvent("onclick", routeUnobtrusive);
|
root.attachEvent("onclick", routeUnobtrusive);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//m.route(route, params, shouldReplaceHistoryEntry)
|
//m.route(route, params, shouldReplaceHistoryEntry)
|
||||||
else if (type.call(arguments[0]) === STRING) {
|
else if (type.call(root) === STRING) {
|
||||||
var oldRoute = currentRoute;
|
var oldRoute = currentRoute;
|
||||||
currentRoute = arguments[0];
|
currentRoute = root;
|
||||||
var args = arguments[1] || {};
|
arg1 || {};
|
||||||
var queryIndex = currentRoute.indexOf("?");
|
var queryIndex = currentRoute.indexOf("?");
|
||||||
var params = queryIndex > -1 ? parseQueryString(currentRoute.slice(queryIndex + 1)) : {};
|
var params = queryIndex > -1 ? parseQueryString(currentRoute.slice(queryIndex + 1)) : {};
|
||||||
for (var i in args) params[i] = args[i];
|
for (var i in arg1) params[i] = arg1[i];
|
||||||
var querystring = buildQueryString(params);
|
var querystring = buildQueryString(params);
|
||||||
var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute;
|
var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute;
|
||||||
if (querystring) currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring;
|
if (querystring) currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring;
|
||||||
|
|
||||||
var shouldReplaceHistoryEntry = (arguments.length === 3 ? arguments[2] : arguments[1]) === true || oldRoute === arguments[0];
|
var shouldReplaceHistoryEntry = (arguments.length === 3 ? arg2 : arg1) === true || oldRoute === root;
|
||||||
|
|
||||||
if (window.history.pushState) {
|
if (window.history.pushState) {
|
||||||
computePreRedrawHook = setScroll;
|
computePreRedrawHook = setScroll;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue