m.route supports params as an object, and builds querystring from it
m.route('/path', {param: 1}) => navigates to '/path?param=1'
The API is a little bit problematic, as it is not possible to combine it with `shouldReplaceHistoryEntry`.
This commit is contained in:
parent
001263080e
commit
5b6b7de045
2 changed files with 27 additions and 9 deletions
21
mithril.js
21
mithril.js
|
|
@ -293,6 +293,9 @@ Mithril = m = new function app(window) {
|
|||
else if (typeof arguments[0] == "string") {
|
||||
currentRoute = arguments[0]
|
||||
var shouldReplaceHistoryEntry = arguments[1] === true
|
||||
var queryString = typeof arguments[1] == "object" ? buildQueryString(arguments[1]) : null
|
||||
if(queryString) currentRoute += (currentRoute.indexOf('?') === -1 ? '?' : '&') + queryString
|
||||
|
||||
if (window.history.pushState) {
|
||||
computePostRedrawHook = function() {
|
||||
window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute)
|
||||
|
|
@ -337,6 +340,14 @@ Mithril = m = new function app(window) {
|
|||
function scrollToHash() {
|
||||
if (m.route.mode != "hash" && window.location.hash) window.location.hash = window.location.hash
|
||||
}
|
||||
function buildQueryString(object, prefix) {
|
||||
var str = []
|
||||
for(var prop in object) {
|
||||
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop]
|
||||
str.push(typeof value == "object" ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
|
||||
}
|
||||
return str.join("&")
|
||||
}
|
||||
function parseQueryString(str) {
|
||||
var pairs = str.split("&"), params = {};
|
||||
for(var i=0; i < pairs.length; i++) {
|
||||
|
|
@ -448,18 +459,10 @@ Mithril = m = new function app(window) {
|
|||
xhr.send(options.data)
|
||||
return xhr
|
||||
}
|
||||
function querystring(object, prefix) {
|
||||
var str = []
|
||||
for(var prop in object) {
|
||||
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop]
|
||||
str.push(typeof value == "object" ? querystring(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
|
||||
}
|
||||
return str.join("&")
|
||||
}
|
||||
function bindData(xhrOptions, data, serialize) {
|
||||
if (data && Object.keys(data).length > 0) {
|
||||
if (xhrOptions.method == "GET") {
|
||||
xhrOptions.url = xhrOptions.url + (xhrOptions.url.indexOf("?") < 0 ? "?" : "&") + querystring(data)
|
||||
xhrOptions.url = xhrOptions.url + (xhrOptions.url.indexOf("?") < 0 ? "?" : "&") + buildQueryString(data)
|
||||
}
|
||||
else xhrOptions.data = serialize(data)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue