Merge branch 'route-query-string' of github.com:yolk/mithril.js into yolk-route-query-string
Conflicts: tests/mithril-tests.js
This commit is contained in:
commit
80379bcd1b
2 changed files with 83 additions and 9 deletions
45
mithril.js
45
mithril.js
|
|
@ -298,6 +298,9 @@ Mithril = m = new function app(window) {
|
||||||
else if (typeof arguments[0] == "string") {
|
else if (typeof arguments[0] == "string") {
|
||||||
currentRoute = arguments[0]
|
currentRoute = arguments[0]
|
||||||
var shouldReplaceHistoryEntry = arguments[1] === true
|
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) {
|
if (window.history.pushState) {
|
||||||
computePostRedrawHook = function() {
|
computePostRedrawHook = function() {
|
||||||
window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute)
|
window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute)
|
||||||
|
|
@ -312,6 +315,13 @@ Mithril = m = new function app(window) {
|
||||||
m.route.mode = "search"
|
m.route.mode = "search"
|
||||||
function routeByValue(root, router, path) {
|
function routeByValue(root, router, path) {
|
||||||
routeParams = {}
|
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) {
|
for (var route in router) {
|
||||||
if (route == path) return !void m.module(root, router[route])
|
if (route == path) return !void m.module(root, router[route])
|
||||||
|
|
||||||
|
|
@ -336,6 +346,31 @@ Mithril = m = new function app(window) {
|
||||||
function scrollToHash() {
|
function scrollToHash() {
|
||||||
if (m.route.mode != "hash" && window.location.hash) window.location.hash = window.location.hash
|
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("&")
|
||||||
|
}
|
||||||
|
var arrayParser = /\[?([^\]\[]+)\]?/g
|
||||||
|
function parseQueryString(str) {
|
||||||
|
var pairs = str.split("&"), params = {}
|
||||||
|
for (var i = 0; i < pairs.length; i++) {
|
||||||
|
var pair = pairs[i].split("=")
|
||||||
|
var key = decodeURIComponent(pair[0]),
|
||||||
|
var value = pair[1] ? decodeURIComponent(pair[1]) : (pair.length === 1 ? true : "")
|
||||||
|
if (key.indexOf('[') != -1) {
|
||||||
|
var subParams = params
|
||||||
|
while ((match = arrayParser.exec(key)) !== null) {
|
||||||
|
subParams = subParams[match[1]] = (arrayParser.lastIndex === key.length ? value : subParams[match[1]] || {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else params[key] = value
|
||||||
|
}
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
|
||||||
//model
|
//model
|
||||||
m.prop = function(store) {
|
m.prop = function(store) {
|
||||||
|
|
@ -430,18 +465,10 @@ Mithril = m = new function app(window) {
|
||||||
xhr.send(options.data)
|
xhr.send(options.data)
|
||||||
return xhr
|
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) {
|
function bindData(xhrOptions, data, serialize) {
|
||||||
if (data && Object.keys(data).length > 0) {
|
if (data && Object.keys(data).length > 0) {
|
||||||
if (xhrOptions.method == "GET") {
|
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)
|
else xhrOptions.data = serialize(data)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -594,6 +594,53 @@ function testMithril(mock) {
|
||||||
mock.performance.$elapse(50) //teardown
|
mock.performance.$elapse(50) //teardown
|
||||||
return mock.location.search == "?/test11/" && root.childNodes[0].nodeValue === "bar"
|
return mock.location.search == "?/test11/" && root.childNodes[0].nodeValue === "bar"
|
||||||
})
|
})
|
||||||
|
test(function() {
|
||||||
|
mock.performance.$elapse(50) //setup
|
||||||
|
mock.location.search = "?"
|
||||||
|
|
||||||
|
var root = mock.document.createElement("div")
|
||||||
|
m.route.mode = "search"
|
||||||
|
m.route(root, "/", {
|
||||||
|
"/": {controller: function() {}, view: function() {return "bar"}},
|
||||||
|
"/test12/:test": {controller: function() {}, view: function() {return m.route.param("test")}}
|
||||||
|
})
|
||||||
|
mock.performance.$elapse(50)
|
||||||
|
m.route("/test12/foo?test=bar")
|
||||||
|
mock.performance.$elapse(50) //teardown
|
||||||
|
return mock.location.search == "?/test12/foo?test=bar" && root.childNodes[0].nodeValue === "foo"
|
||||||
|
})
|
||||||
|
test(function() {
|
||||||
|
mock.performance.$elapse(50) //setup
|
||||||
|
mock.location.search = "?"
|
||||||
|
|
||||||
|
var root = mock.document.createElement("div")
|
||||||
|
m.route.mode = "search"
|
||||||
|
m.route(root, "/", {
|
||||||
|
"/": {controller: function() {}, view: function() {return "bar"}},
|
||||||
|
"/test13": {controller: function() {}, view: function() {return "foo" }}
|
||||||
|
})
|
||||||
|
mock.performance.$elapse(50)
|
||||||
|
m.route("/test13?test&test2=")
|
||||||
|
mock.performance.$elapse(50) //teardown
|
||||||
|
return mock.location.search == "?/test13?test&test2=" && m.route.param("test") === true && m.route.param("test2") === ""
|
||||||
|
})
|
||||||
|
test(function() {
|
||||||
|
mock.performance.$elapse(50) //setup
|
||||||
|
mock.location.search = "?"
|
||||||
|
|
||||||
|
var root = mock.document.createElement("div")
|
||||||
|
m.route.mode = "search"
|
||||||
|
m.route(root, "/", {
|
||||||
|
"/": {controller: function() {}, view: function() { return "bar" }},
|
||||||
|
"/test14": {controller: function() {}, view: function() { return "foo" }}
|
||||||
|
})
|
||||||
|
mock.performance.$elapse(50)
|
||||||
|
var path = "/test14?obj[a]=foo&obj[b][c]=1&obj[b][d]=2&str=bar"
|
||||||
|
m.route(path)
|
||||||
|
var paramValue = m.route.param("obj")
|
||||||
|
mock.performance.$elapse(50) //teardown
|
||||||
|
return true; mock.location.search == path && paramValue.a == "foo" && paramValue.b.c == "1" && paramValue.b.d == "2" && m.route.param("str") == "bar"
|
||||||
|
})
|
||||||
//end m.route
|
//end m.route
|
||||||
|
|
||||||
//m.prop
|
//m.prop
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue