diff --git a/docs/v1.x-migration.md b/docs/v1.x-migration.md index f656fc01..8bb02eff 100644 --- a/docs/v1.x-migration.md +++ b/docs/v1.x-migration.md @@ -139,7 +139,7 @@ var component = { } }; -m("div", m(component, { fooga : 1 })); +m("div", m.component(component, { fooga : 1 })); ``` ### `v1.x` diff --git a/mithril.js b/mithril.js index 8bb84dc3..ecaef3f2 100644 --- a/mithril.js +++ b/mithril.js @@ -86,6 +86,7 @@ function changeNS(ns, vnode) { var m = hyperscript var renderService = function($window) { var $doc = $window.document + var $emptyFragment = $doc.createDocumentFragment() var onevent function setEventCallback(callback) {return onevent = callback} @@ -172,10 +173,13 @@ var renderService = function($window) { initLifecycle(vnode.tag, vnode, hooks) vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode)) - var element = createNode(vnode.instance, hooks) - vnode.dom = vnode.instance.dom - vnode.domSize = vnode.instance.domSize - return element + if (vnode.instance != null) { + var element = createNode(vnode.instance, hooks) + vnode.dom = vnode.instance.dom + vnode.domSize = vnode.instance.domSize + return element + } + else return $emptyFragment } //update function updateNodes(parent, old, vnodes, hooks, nextSibling) { @@ -311,9 +315,11 @@ var renderService = function($window) { function updateComponent(parent, old, vnode, hooks, nextSibling, recycling) { vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode)) updateLifecycle(vnode.tag, vnode, hooks, recycling) - updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling) - vnode.dom = vnode.instance.dom - vnode.domSize = vnode.instance.domSize + if (vnode.instance != null) { + updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling) + vnode.dom = vnode.instance.dom + vnode.domSize = vnode.instance.domSize + } } function isRecyclable(old, vnodes) { if (old.pool != null && Math.abs(old.pool.length - vnodes.length) <= Math.abs(old.length - vnodes.length)) { @@ -595,9 +601,9 @@ var buildQueryString = function(object) { else args.push(encodeURIComponent(key) + (value != null && value !== "" ? "=" + encodeURIComponent(value) : "")) } } -m.request = function($window, Promise) { +var requestService = function($window, Promise) { var callbackCount = 0 - function ajax(args) { + function xhr(args) { return new Promise(function(resolve, reject) { var useBody = args.useBody != null ? args.useBody : args.method !== "GET" && args.method !== "TRACE" @@ -632,7 +638,7 @@ m.request = function($window, Promise) { response[i] = new args.type(response[i]) } } - else response = new args.type(response[i]) + else response = new args.type(response) } resolve(response) @@ -651,21 +657,21 @@ m.request = function($window, Promise) { } function jsonp(args) { return new Promise(function(resolve, reject) { - var callbackKey = "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ + var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ var script = $window.document.createElement("script") - $window[callbackKey] = function(data) { + $window[callbackName] = function(data) { script.parentNode.removeChild(script) resolve(data) - $window[callbackKey] = undefined + delete $window[callbackName] } script.onerror = function() { script.parentNode.removeChild(script) reject(new Error("JSONP request failed")) - $window[callbackKey] = undefined + delete $window[callbackName] } if (args.data == null) args.data = {} args.url = interpolate(args.url, args.data) - args.data[args.callbackKey || "callback"] = callbackKey + args.data[args.callbackKey || "callback"] = callbackName script.src = assemble(args.url, args.data) $window.document.documentElement.appendChild(script) }) @@ -697,8 +703,10 @@ m.request = function($window, Promise) { } function extract(xhr) {return xhr.responseText} - return {ajax: ajax, jsonp: jsonp} -}(window, Promise).ajax + return {xhr: xhr, jsonp: jsonp} +}(window, Promise) +m.request = requestService.xhr +m.jsonp = requestService.jsonp var parseQueryString = function(string) { if (string === "" || string == null) return {} if (string.charAt(0) === "?") string = string.slice(1) @@ -771,7 +779,7 @@ var coreRouter = function($window) { switch (type) { case "#": return normalize("hash").slice(prefix.length) case "?": return normalize("search").slice(prefix.length) + normalize("hash") - default: return normalize("pathname") + normalize("search") + normalize("hash") + default: return normalize("pathname").slice(prefix.length) + normalize("search") + normalize("hash") } } function setPath(path, data, options) { @@ -909,7 +917,9 @@ m.prop = function(store) { } } m.withAttr = function(attrName, callback, context) { - return callback.call(context || this, attrName in e.currentTarget ? e.currentTarget[attrName] : e.currentTarget.getAttribute(attrName)) + return function(e) { + return callback.call(context || this, attrName in e.currentTarget ? e.currentTarget[attrName] : e.currentTarget.getAttribute(attrName)) + } } m.render = renderService.render m.redraw = redrawService.publish diff --git a/package.json b/package.json index fb0bc573..2decdd87 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,8 @@ "name": "mithril", "version": "1.0.0", "description": "A framework for building brilliant applications", + "author": "Leo Horie", + "license": "MIT", "main": "index.js", "scripts": { "build": "node bundler/bundler", @@ -9,10 +11,11 @@ "test": "node ospec/bin/ospec", "cover": "istanbul cover --print both ospec/bin/ospec" }, - "author": "Leo Horie", - "license": "MIT", "devDependencies": { "eslint": "^2.10.2", "istanbul": "^0.4.3" + }, + "publishConfig": { + "tag": "beta" } } diff --git a/router/router.js b/router/router.js index 446edd06..f6911de0 100644 --- a/router/router.js +++ b/router/router.js @@ -36,7 +36,7 @@ module.exports = function($window) { switch (type) { case "#": return normalize("hash").slice(prefix.length) case "?": return normalize("search").slice(prefix.length) + normalize("hash") - default: return normalize("pathname") + normalize("search") + normalize("hash") + default: return normalize("pathname").slice(prefix.length) + normalize("search") + normalize("hash") } } diff --git a/router/tests/test-defineRoutes.js b/router/tests/test-defineRoutes.js index bb6ba98e..85cb52db 100644 --- a/router/tests/test-defineRoutes.js +++ b/router/tests/test-defineRoutes.js @@ -5,7 +5,7 @@ var pushStateMock = require("../../test-utils/pushStateMock") var Router = require("../../router/router") o.spec("Router.defineRoutes", function() { - void ["#", "?", "", "#!", "?!"].forEach(function(prefix) { + void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) { o.spec("using prefix `" + prefix + "`", function() { var $window, router, onRouteChange, onFail diff --git a/router/tests/test-getPath.js b/router/tests/test-getPath.js index fcfeaba5..70e2fb7b 100644 --- a/router/tests/test-getPath.js +++ b/router/tests/test-getPath.js @@ -5,7 +5,7 @@ var pushStateMock = require("../../test-utils/pushStateMock") var Router = require("../../router/router") o.spec("Router.getPath", function() { - void ["#", "?", "", "#!", "?!"].forEach(function(prefix) { + void ["#", "?", "", "#!", "?!", '/foo'].forEach(function(prefix) { o.spec("using prefix `" + prefix + "`", function() { var $window, router, onRouteChange, onFail diff --git a/router/tests/test-setPath.js b/router/tests/test-setPath.js index b85c97b2..a54db6f8 100644 --- a/router/tests/test-setPath.js +++ b/router/tests/test-setPath.js @@ -5,7 +5,7 @@ var pushStateMock = require("../../test-utils/pushStateMock") var Router = require("../../router/router") o.spec("Router.setPath", function() { - void ["#", "?", "", "#!", "?!"].forEach(function(prefix) { + void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) { o.spec("using prefix `" + prefix + "`", function() { var $window, router, onRouteChange, onFail @@ -78,8 +78,10 @@ o.spec("Router.setPath", function() { router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail) router.setPath("/other", null, {replace: false}) $window.history.back() - - o($window.location.href).equals("http://localhost/" + (prefix ? prefix + "/" : "") + "test") + + var slash = prefix[0] === "/" ? "" : "/" + + o($window.location.href).equals("http://localhost" + slash + (prefix ? prefix + "/" : "") + "test") }) }) })