Add query string support to m.route

m.route now parses query string in given path into m.route.param

	* Params gathered the old way (e.g. /test/:id) overwrite params from the query string
	* /?valid => m.route.param('valid') === true
	* /?blank= => in m.route.param('blank') === ''
	* Supports nested values: ?test[a][b]=1&test[a][b]=2 => m.route.param('a') == {b: "1", c: "2"}

The nested values where only added to behave similar to the encoding function querystring in mithril. Maybe this is not necessary? Code could be shorter.
This commit is contained in:
Sebastian 2014-05-22 15:55:31 +02:00
parent 67c5400d44
commit 001263080e
2 changed files with 87 additions and 0 deletions

View file

@ -307,6 +307,13 @@ Mithril = m = new function app(window) {
m.route.mode = "search"
function routeByValue(root, router, path) {
routeParams = {}
var queryStart = path.indexOf('?');
if (queryStart !== -1) {
routeParams = parseQueryString(path.substr(queryStart + 1, path.length));
path = path.substr(0, queryStart);
}
for (var route in router) {
if (route == path) return !void m.module(root, router[route])
@ -330,6 +337,24 @@ Mithril = m = new function app(window) {
function scrollToHash() {
if (m.route.mode != "hash" && window.location.hash) window.location.hash = window.location.hash
}
function parseQueryString(str) {
var pairs = str.split("&"), params = {};
for(var i=0; i < pairs.length; i++) {
var pair = pairs[i].split("="),
key = decodeURIComponent(pair[0]),
value = pair[1] ? decodeURIComponent(pair[1]) : (pair.length === 1 ? true : "");
if (key.indexOf('[') != -1) {
var e, regex = /\[?([^\]\[]+)\]?/g,
subParams = params;
while ((e = regex.exec(key)) !== null) {
subParams = subParams[e[1]] = (regex.lastIndex === key.length ? value : subParams[e[1]] || {})
}
} else {
params[key] = value;
}
}
return params;
}
//model
m.prop = function(store) {