diff --git a/docs/mithril.computation.md b/docs/mithril.computation.md index b625e34e..b7c7d2d9 100644 --- a/docs/mithril.computation.md +++ b/docs/mithril.computation.md @@ -2,7 +2,7 @@ --- -- [How auto-redrawing-works](#how-auto-redrawing-works) +- [How auto-redrawing works](#how-auto-redrawing-works) - [Integrating multiple execution threads](#integrating-multiple-execution-threads) - [Integrating to legacy code](#integrating-to-legacy-code) - [Signature](#signature) @@ -242,4 +242,4 @@ void startComputation() ```clike void endComputation() -``` \ No newline at end of file +``` diff --git a/docs/practices.md b/docs/practices.md index 172531b3..71545a82 100644 --- a/docs/practices.md +++ b/docs/practices.md @@ -68,32 +68,6 @@ That organization pattern needlessly ties unrelated aspects of the application t --- -## Global Namespace Hygiene - -For developer convenience, Mithril uses the global `m` variable as a namespace, much like jQuery uses `$`. - -If you want to ensure global namespace hygiene, you can wrap your code in "islands" like this: - -```javascript -new function(m) { - - //your code goes here - -}(Mithril); -``` - -If you are creating components to be used by 3rd parties, it's recommended that you always use this idiom. - -In the unlikely case that you have another global variable called `m` in your page, you should consider renaming it to make it more descriptive, or use the idiom below to keep it intact. - -```markup - - - -``` - ---- - ## Usage of m.redraw `m.redraw` is a method that allows you to render a template outside the scope of Mithril's auto-redrawing system. diff --git a/mithril.js b/mithril.js index 5add8502..2da6e11f 100644 --- a/mithril.js +++ b/mithril.js @@ -1,4 +1,4 @@ -Mithril = m = new function app(window, undefined) { +var m = (function app(window, undefined) { var OBJECT = "[object Object]", ARRAY = "[object Array]", STRING = "[object String]", FUNCTION = "function"; var type = {}.toString; var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g, attrParser = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/; @@ -362,7 +362,8 @@ Mithril = m = new function app(window, undefined) { function clear(nodes, cached) { for (var i = nodes.length - 1; i > -1; i--) { if (nodes[i] && nodes[i].parentNode) { - nodes[i].parentNode.removeChild(nodes[i]); + try {nodes[i].parentNode.removeChild(nodes[i])} + catch (e) {} //ignore if this fails due to order of events (see http://stackoverflow.com/questions/21926083/failed-to-execute-removechild-on-node) cached = [].concat(cached); if (cached[i]) unload(cached[i]) } @@ -580,11 +581,16 @@ Mithril = m = new function app(window, undefined) { element.removeEventListener("click", routeUnobtrusive); element.addEventListener("click", routeUnobtrusive) } - //m.route(route) + //m.route(route, params) else if (type.call(arguments[0]) == STRING) { currentRoute = arguments[0]; - var querystring = arguments[1] != null && type.call(arguments[1]) == OBJECT ? buildQueryString(arguments[1]) : null; - if (querystring) currentRoute += (currentRoute.indexOf("?") === -1 ? "?" : "&") + querystring; + var args = arguments[1] || {} + var queryIndex = currentRoute.indexOf("?") + var params = queryIndex > -1 ? parseQueryString(currentRoute.slice(queryIndex + 1)) : {} + for (var i in args) params[i] = args[i] + var querystring = buildQueryString(params) + var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute + if (querystring) currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring; var shouldReplaceHistoryEntry = (arguments.length == 3 ? arguments[2] : arguments[1]) === true; @@ -654,7 +660,7 @@ Mithril = m = new function app(window, undefined) { var pairs = str.split("&"), params = {}; for (var i = 0; i < pairs.length; i++) { var pair = pairs[i].split("="); - params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : (pair.length === 1 ? true : "") + params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : "" } return params } @@ -971,7 +977,7 @@ Mithril = m = new function app(window, undefined) { m.deps.factory = app; return m -}(typeof window != "undefined" ? window : {}); +})(typeof window != "undefined" ? window : {}); -if (typeof module != "undefined" && module !== null) module.exports = m; -if (typeof define == "function" && define.amd) define(function() {return m}); +if (typeof module != "undefined" && module !== null && module.exports) module.exports = m; +else if (typeof define == "function" && define.amd) define(function() {return m}); diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index de9f97be..4291bf1f 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -1063,7 +1063,7 @@ function testMithril(mock) { mock.requestAnimationFrame.$resolve() m.route("/test14?test&test2=") mock.requestAnimationFrame.$resolve() //teardown - return mock.location.search == "?/test14?test&test2=" && m.route.param("test") === true && m.route.param("test2") === "" + return mock.location.search == "?/test14?test=&test2=" && m.route.param("test") === "" && m.route.param("test2") === "" }) test(function() { mock.requestAnimationFrame.$resolve() //setup @@ -1597,6 +1597,30 @@ function testMithril(mock) { return root.childNodes[0].nodeValue == "b" }) + test(function() { + mock.requestAnimationFrame.$resolve() + mock.location.search = "?" + + var root = mock.document.createElement("div") + + var a = {} + a.controller = function() { + m.route("/b?foo=1", {foo: 2}) + } + a.view = function() {return "a"} + + var b = {} + b.controller = function() {} + b.view = function() {return "b"} + + m.route(root, "/", { + "/": a, + "/b": b, + }) + mock.requestAnimationFrame.$resolve() + + return mock.location.search == "?/b?foo=2" + }) //end m.route //m.prop