From 6170573c290e980987cb1b1d9b78631e1b633f83 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Thu, 5 Jan 2017 23:07:34 -0800 Subject: [PATCH 1/7] feat: Return empty string node for `false` values Very specifically doing a strict `false` check here to try & avoid coercion perf costs. --- render/vnode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render/vnode.js b/render/vnode.js index 99877e4e..56df8c81 100644 --- a/render/vnode.js +++ b/render/vnode.js @@ -3,7 +3,7 @@ function Vnode(tag, key, attrs, children, text, dom) { } Vnode.normalize = function(node) { if (Array.isArray(node)) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined) - if (node != null && typeof node !== "object") return Vnode("#", undefined, undefined, node, undefined, undefined) + if (node != null && typeof node !== "object") return Vnode("#", undefined, undefined, node === false ? "" : node, undefined, undefined) return node } Vnode.normalizeChildren = function normalizeChildren(children) { From 9f32267259a51db1f3a0dcc8161cf8f219078eec Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Thu, 5 Jan 2017 23:07:47 -0800 Subject: [PATCH 2/7] tests: Update tests for false -> "" behavior --- render/tests/test-component.js | 6 +++--- render/tests/test-hyperscript.js | 12 +++++++++--- render/tests/test-normalize.js | 4 ++-- render/tests/test-normalizeChildren.js | 6 ++++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/render/tests/test-component.js b/render/tests/test-component.js index f4aad629..4bb0a3b6 100644 --- a/render/tests/test-component.js +++ b/render/tests/test-component.js @@ -104,7 +104,7 @@ o.spec("component", function() { visible = false render(root, [{tag: component}]) - o(root.firstChild.nodeValue).equals("false") + o(root.firstChild.nodeValue).equals("") }) o("updates root from null to null", function() { var component = { @@ -232,7 +232,7 @@ o.spec("component", function() { render(root, [{tag: component}]) o(root.firstChild.nodeType).equals(3) - o(root.firstChild.nodeValue).equals("false") + o(root.firstChild.nodeValue).equals("") }) o("can return null", function() { var component = { @@ -668,7 +668,7 @@ o.spec("component", function() { function init(vnode) { o(vnode.state.data).deepEquals(data) o(vnode.state.data).equals(data) - + //inherits state via prototype component.x = 1 o(vnode.state.x).equals(1) diff --git a/render/tests/test-hyperscript.js b/render/tests/test-hyperscript.js index 8237d0ea..32e767d8 100644 --- a/render/tests/test-hyperscript.js +++ b/render/tests/test-hyperscript.js @@ -229,7 +229,7 @@ o.spec("hyperscript", function() { o("handles falsy boolean single child", function() { var vnode = m("div", {}, [false]) - o(vnode.text).equals(false) + o(vnode.text).equals("") }) o("handles null single child", function() { var vnode = m("div", {}, [null]) @@ -261,7 +261,7 @@ o.spec("hyperscript", function() { var vnode = m("div", {}, [false, true]) o(vnode.children[0].tag).equals("#") - o(vnode.children[0].children).equals(false) + o(vnode.children[0].children).equals("") o(vnode.children[1].tag).equals("#") o(vnode.children[1].children).equals(true) }) @@ -353,10 +353,16 @@ o.spec("hyperscript", function() { o(vnode.text).equals(true) }) o("handles attr and single falsy boolean text child", function() { + var vnode = m("div", {a: "b"}, [0]) + + o(vnode.attrs.a).equals("b") + o(vnode.text).equals(0) + }) + o("handles attr and single false boolean text child", function() { var vnode = m("div", {a: "b"}, [false]) o(vnode.attrs.a).equals("b") - o(vnode.text).equals(false) + o(vnode.text).equals("") }) o("handles attr and single text child unwrapped", function() { var vnode = m("div", {a: "b"}, "c") diff --git a/render/tests/test-normalize.js b/render/tests/test-normalize.js index 7a01af31..237612a4 100644 --- a/render/tests/test-normalize.js +++ b/render/tests/test-normalize.js @@ -48,10 +48,10 @@ o.spec("normalize", function() { o(node.tag).equals("#") o(node.children).equals(true) }) - o("normalizes falsy boolean into text node", function() { + o("normalizes falsy boolean into empty text node", function() { var node = Vnode.normalize(false) o(node.tag).equals("#") - o(node.children).equals(false) + o(node.children).equals("") }) }) diff --git a/render/tests/test-normalizeChildren.js b/render/tests/test-normalizeChildren.js index 9ad2cd89..699f0eab 100644 --- a/render/tests/test-normalizeChildren.js +++ b/render/tests/test-normalizeChildren.js @@ -16,4 +16,10 @@ o.spec("normalizeChildren", function() { o(children[0].tag).equals("#") o(children[0].children).equals("a") }) + o("normalizes `false` values into empty string text nodes", function() { + var children = Vnode.normalizeChildren([false]) + + o(children[0].tag).equals("#") + o(children[0].children).equals("") + }) }) From 6b15378c41e9a4d56a9941e5928e30e03ac0aed7 Mon Sep 17 00:00:00 2001 From: Gandalf-the-Bot Date: Sat, 7 Jan 2017 03:36:49 +0000 Subject: [PATCH 3/7] Bundled output for commit 74ded82165eac505b4a504c33388bd4d59d48a3e [skip ci] --- README.md | 2 +- mithril.js | 6 +--- mithril.min.js | 83 +++++++++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 466958b4..b6e99098 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,6 @@ There are over 4000 assertions in the test suite, and tests cover even difficult ## Modularity -Despite the huge improvements in performance and modularity, the new codebase is smaller than v0.2.x, currently clocking at 7.58 KB min+gzip +Despite the huge improvements in performance and modularity, the new codebase is smaller than v0.2.x, currently clocking at 7.59 KB min+gzip In addition, Mithril is now completely modular: you can import only the modules that you need and easily integrate 3rd party modules if you wish to use a different library for routing, ajax, and even rendering diff --git a/mithril.js b/mithril.js index 29e9e17c..522c546d 100644 --- a/mithril.js +++ b/mithril.js @@ -430,11 +430,7 @@ var coreRenderer = function($window) { return element } function createComponent(vnode, hooks, ns) { - // For object literals since `Vnode()` always sets the `state` field. - if (!vnode.state) vnode.state = {} - var constructor = function() {} - constructor.prototype = vnode.tag - vnode.state = new constructor + vnode.state = Object.create(vnode.tag) var view = vnode.tag.view if (view.reentrantLock != null) return $emptyFragment view.reentrantLock = true diff --git a/mithril.min.js b/mithril.min.js index 71b834ca..ff483e40 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,42 +1,41 @@ -new function(){function w(b,c,h,d,g,l){return{tag:b,key:c,attrs:h,children:d,text:g,dom:l,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function A(b){if(null==b||"string"!==typeof b&&null==b.view)throw Error("The selector must be either a string or a component.");if("string"===typeof b&&void 0===G[b]){for(var c,h,d=[],g={};c=N.exec(b);){var l=c[1],m=c[2];""===l&&""!==m?h=m:"#"===l?g.id=m:"."===l?d.push(m):"["===c[3][0]&&((l=c[6])&&(l=l.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")), -"class"===c[4]?d.push(l):g[c[4]]=l||!0)}0a.indexOf("?")?"?":"&";a+=d+c}return a}function m(b){try{return""!==b?JSON.parse(b):null}catch(C){throw Error(b);}}function q(b){return b.responseText} -function n(b,c){if("function"===typeof b)if(Array.isArray(c))for(var a=0;ak.status||304===k.status)c(n(a.type,b));else{var g=Error(k.responseText),e;for(e in b)g[e]=b[e];d(g)}}catch(f){d(f)}};h&&null!=a.data?k.send(a.data):k.send()}); -return!0===a.background?z:t(z)},jsonp:function(a,m){var t=h();a=d(a,m);var q=new c(function(c,d){var h=a.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+k++,m=b.document.createElement("script");b[h]=function(d){m.parentNode.removeChild(m);c(n(a.type,d));delete b[h]};m.onerror=function(){m.parentNode.removeChild(m);d(Error("JSONP request failed"));delete b[h]};null==a.data&&(a.data={});a.url=g(a.url,a.data);a.data[a.callbackKey||"callback"]=h;m.src=l(a.url,a.data);b.document.documentElement.appendChild(m)}); -return!0===a.background?q:t(q)},setCompletionCallback:function(b){t=b}}}(window,x),M=function(b){function c(e,f,b,a,c,d,g){for(;b=u&&z>=t;){var y=f[u],p=b[t];if(y!==p||r)if(null==y)u++;else if(null==p)t++;else if(y.key===p.key)u++,t++,m(e,y,p,d,n(f,u,g),r,l),r&&y.tag===p.tag&&k(e,q(y),g);else if(y=f[v],y!==p||r)if(null==y)v--;else if(null==p)t++;else if(y.key===p.key)m(e, -y,p,d,n(f,v+1,g),r,l),(r||t=u&&z>=t;){y=f[v];p=b[z];if(y!==p||r)if(null==y)v--;else{if(null!=p)if(y.key===p.key)m(e,y,p,d,n(f,v+1,g),r,l),r&&y.tag===p.tag&&k(e,q(y),g),null!=y.dom&&(g=y.dom),v--;else{if(!C){C=f;var y=v,E={},w;for(w=0;wb.indexOf("?")?"?":"&";b+=d+c}return b}function q(a){try{return""!==a?JSON.parse(a):null}catch(r){throw Error(a);}}function l(a){return a.responseText} +function t(a,c){if("function"===typeof a)if(Array.isArray(c))for(var b=0;bn.status||304===n.status)c(t(b.type,a));else{var g=Error(n.responseText),e;for(e in a)g[e]=a[e];d(g)}}catch(f){d(f)}};k&&null!=b.data?n.send(b.data):n.send()}); +return!0===b.background?r:z(r)},jsonp:function(b,l){var q=k();b=d(b,l);var z=new c(function(c,d){var k=b.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+p++,l=a.document.createElement("script");a[k]=function(d){l.parentNode.removeChild(l);c(t(b.type,d));delete a[k]};l.onerror=function(){l.parentNode.removeChild(l);d(Error("JSONP request failed"));delete a[k]};null==b.data&&(b.data={});b.url=h(b.url,b.data);b.data[b.callbackKey||"callback"]=k;l.src=m(b.url,b.data);a.document.documentElement.appendChild(l)}); +return!0===b.background?z:q(z)},setCompletionCallback:function(a){z=a}}}(window,x),M=function(a){function c(g,e,a,b,c,d,h){for(;a=u&&r>=n;){var y=e[u],v=a[n];if(y!==v||f)if(null==y)u++;else if(null==v)n++;else if(y.key===v.key)u++,n++,m(g,y,v,b,l(e,u,d),f,h),f&&y.tag===v.tag&&t(g,q(y),d);else if(y=e[p],y!==v||f)if(null==y)p--;else if(null==v)n++;else if(y.key===v.key)m(g,y,v,b,l(e,p+1,d),f,h),(f||n=u&&r>=n;){y=e[p];v=a[r];if(y!==v||f)if(null==y)p--;else{if(null!=v)if(y.key===v.key)m(g,y,v,b,l(e,p+1,d),f,h),f&&y.tag===v.tag&&t(g,q(y),d),null!=y.dom&&(d=y.dom),p--;else{if(!A){A=e;var y=p,C={},w;for(w=0;w Date: Sat, 7 Jan 2017 12:04:30 -0500 Subject: [PATCH 4/7] autoredraw docs --- docs/autoredraw.md | 120 +++++++++++++++++++++++++++++++++++++++++++++ docs/guides.md | 1 + 2 files changed, 121 insertions(+) create mode 100644 docs/autoredraw.md diff --git a/docs/autoredraw.md b/docs/autoredraw.md new file mode 100644 index 00000000..ec4c5945 --- /dev/null +++ b/docs/autoredraw.md @@ -0,0 +1,120 @@ +# The auto-redraw system + +Mithril implements a virtual DOM diffing system for fast rendering, and in addition, it offers various mechanisms to gain granular control over the rendering of an application. + +When used idiomatically, Mithril employs an auto-redraw system that synchronizes the DOM whenever changes are made in the data layer. The auto-redraw system becomes enabled when you call `m.mount` or `m.route` (but it stays disabled if your app is bootstrapped solely via `m.render` calls). + +The auto-redraw system simply consists of triggering a re-render function behind the scenes after certain functions complete. + +### After event handlers + +Mithril automatically redraws after DOM event handlers that are defined in a Mithril view: + +```javascript +var MyComponent = { + view: function() { + return m("div", {onclick: doSomething}) + } +} + +function doSomething() { + //a redraw happens synchronously after this function runs +} + +m.mount(document.body, MyComponent) +``` + +You can disable an auto-redraw for specific events by setting `e.redraw` to `false`. + +```javascript +var MyComponent = { + view: function() { + return m("div", {onclick: doSomething}) + } +} + +function doSomething(e) { + e.redraw = false + // no longer triggers a redraw when the div is clicked +} + +m.mount(document.body, MyComponent) +``` + + +### After m.request + +Mithril automatically redraws after [`m.request`](request.md) completes: + +```javascript +m.request("/api/v1/users").then(function() { + //a redraw happens after this function runs +}) +``` + +You can disable an auto-redraw for a specific request by setting the `background` option to true: + +```javascript +m.request("/api/v1/users", {background: true}).then(function() { + //does not trigger a redraw +}) +``` + + +### After route changes + +Mithril automatically redraws after [`m.route.set()`](route.md#routeset) calls (or route changes via links that use [`m.route.link`](route.md#routelink) + +```javascript +var RoutedComponent = { + view: function() { + return [ + // a redraw happens asynchronously after the route changes + m("a", {href: "/", oncreate: m.route.link}), + m("div", { + onclick: function() { + m.route.set("/") + } + }), + ] + } +} + +m.route(document.body, "/", { + "/": RoutedComponent, +}) +``` + +--- + +### When Mithril does not redraws + +Mithril does not redraw after 3rd party library event handlers. In those cases, you must manually call [`m.redraw()`](redraw.md). + +Mithril also does not redraw after lifecycle methods. This is because lifecycle methods run within the redraw cycle and allowing a nested redraw to run could cause loss of stability or even stack overflows. If you need to trigger a redraw within a lifecycle method, you should call `m.redraw` from within the callback of an asynchronous function such as `requestAnimationFrame`, `Promise.resolve` or `setTimeout`. + +```javascript +var StableComponent = { + oncreate: function(vnode) { + vnode.state.height = vnode.dom.offsetHeight + requestAnimationFrame(function() { + m.redraw() + }) + }, + view: function() { + return m("div", "This component is " + vnode.state.height + "px tall") + } +} +``` + +Mithril does not auto-redraw vnode trees that are rendered via `m.render`. This means redraws do not occur after event changes and `m.request` calls for templates that were rendered via `m.render`. Thus, if your architecture requires manual control over when rendering occurs (as can sometimes be the case when using libraries like Redux), you should use `m.render` instead of `m.mount`. + +Remember that `m.render` expects a vnode tree, and `m.mount` expects a component: + +```javascript +// wrap the component in a m() call for m.render +m.render(document.body, m(MyComponent)) + +// don't wrap the component for m.mount +m.mount(document.body, MyComponent) +``` diff --git a/docs/guides.md b/docs/guides.md index d2bd87da..1e05cc3b 100644 --- a/docs/guides.md +++ b/docs/guides.md @@ -9,6 +9,7 @@ - [Components](components.md) - [Lifecycle methods](lifecycle-methods.md) - [Keys](keys.md) + - [Autoredraw system](autoredraw.md) - Social - [Community chat](https://gitter.im/lhorie/mithril.js) - [Mithril Jobs](https://github.com/lhorie/mithril.js/wiki/JOBS) From 6284a3bd86e3bf126e62c9b9c16dfd8138a9edb5 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 7 Jan 2017 14:54:48 -0500 Subject: [PATCH 5/7] es6 and jsx docs --- docs/es6.md | 69 +++++++++++++++++ docs/guides.md | 2 + docs/introduction.md | 9 +-- docs/jsx.md | 177 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 250 insertions(+), 7 deletions(-) create mode 100644 docs/es6.md create mode 100644 docs/jsx.md diff --git a/docs/es6.md b/docs/es6.md new file mode 100644 index 00000000..e8a22000 --- /dev/null +++ b/docs/es6.md @@ -0,0 +1,69 @@ +# ES6 + +Mithril is written in ES5, and is fully compatible with ES6 as well. + +In some limited environments, it's possible to use a significant subset of ES6 directly without extra tooling (for example, in internal applications that do not support IE). However, for the vast majority of use cases, a compiler toolchain like [Babel](https://babeljs.io) is required to compile ES6 features down to ES5. + +### Setup + +The simplest way to setup an ES6 compilation toolchain is via [Babel](https://babeljs.io/). To install, use this command: + +```bash +npm install babel-cli babel-preset-es2015 transform-react-jsx --save-dev +``` + +Create a `.babelrc` file: + +``` +{ + "presets": ["es2015"], + "plugins": [ + ["transform-react-jsx", { + "pragma": "m" + }] + ] +} +``` + +To run Babel as a standalone tool, run this from the command line: + +```bash +babel src --out-dir lib --source-maps +``` + +#### Using Babel with Webpack + +If you're using Webpack as a bundler, you can integrate Babel to Webpack, however this requires some additional dependencies, in addition to the steps above. + +```bash +npm install babel-core babel-loader --save-dev +``` + +Create a file called `.webpack.config` + +```javascript +module.exports = { + entry: './src/index.js', + output: { + path: './bin', + filename: 'app.js', + }, + module: { + loaders: [{ + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel-loader' + }] + } +} +``` + +--- + +### Custom setups + +If you're using Webpack, you can [follow its excellent guide to add support for ES6](https://webpack.github.io/docs/usage.html#transpiling-es2015-using-babel-loader) + +If you want to use Babel as a standalone tool, [here's the instructions for how to set it up](https://babeljs.io/docs/setup/#installation). + +[Google closure compiler](https://www.npmjs.com/package/google-closure-compiler) is another tool that supports ES6 to ES5 compilation. diff --git a/docs/guides.md b/docs/guides.md index 1e05cc3b..1c256266 100644 --- a/docs/guides.md +++ b/docs/guides.md @@ -2,6 +2,8 @@ - [Installation](installation.md) - [Introduction](introduction.md) - [Tutorial](simple-application.md) + - [JSX](jsx.md) + - [ES6](es6.md) - [Testing](testing.md) - [Examples](examples.md) - Key concepts diff --git a/docs/introduction.md b/docs/introduction.md index 29c59a4e..b98dfb45 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -25,7 +25,7 @@ Note: This introduction assumes you have basic level of Javacript knowledge. If The easiest way to try out Mithril is to include it from a CDN, and follow this tutorial. It'll cover the majority of the API surface but it'll only take 10 minutes. -Let's create an HTML file to follow along: +Let's create an HTML file to follow along: ```markup @@ -91,7 +91,7 @@ m("main", [ ]) ``` -Note: If you prefer `` syntax, [it's possible to use it via a Babel plugin](https://babeljs.io/docs/plugins/transform-react-jsx/). +Note: If you prefer `` syntax, [it's possible to use it via a Babel plugin](jsx.md). ```markup // HTML syntax via Babel's JSX plugin @@ -236,8 +236,3 @@ Clicking the button should now update the count. We covered how to create and update HTML, how to create components, routes for a Single Page Application, and interacted with a server via XHR. This should be enough to get you started writing the frontend for a real application. Now that you are comfortable with the basics of the Mithril API, [be sure to check out the simple application tutorial](simple-application.md), which walks you through building a realistic application. - - - - - diff --git a/docs/jsx.md b/docs/jsx.md new file mode 100644 index 00000000..819d8315 --- /dev/null +++ b/docs/jsx.md @@ -0,0 +1,177 @@ +# JSX + +- [Description](#description) +- [Setup](#setup) +- [JSX vs hyperscript](#jsx-vs-hyperscript) +- [Converting HTML](#converting-html) + +--- + +### Description + +JSX is a syntax extension that enables you to write HTML tags interspersed with Javascript. + +``` +var MyComponent = { + view: function() { + return m("main", [ + m("h1", "Hello world"), + ]) + } +} + +// can be written as: +var MyComponent = { + view: function() { + return ( +
+

Hello world

+
+ ) + } +} +``` + +When using JSX, it's possible to interpolate Javascript expressions within JSX tags by using curly braces: + +``` +var greeting = "Hello" +var url = "http://google.com" +var div = {greeting + "!"} +``` + +Components can be used by using a convention of uppercasing the first letter of the component name: + +```javascript +m.mount(document.body, ) +// equivalent to m.mount(document.body, m(Component)) +``` + +--- + +### Setup + +The simplest way to use JSX is via a [Babel](https://babeljs.io/) plugin. To install, use this command: + +```bash +npm install babel-cli babel-preset-es2015 transform-react-jsx --save-dev +``` + +Create a `.babelrc` file: + +``` +{ + "presets": ["es2015"], + "plugins": [ + ["transform-react-jsx", { + "pragma": "m" + }] + ] +} +``` + +To run Babel as a standalone tool, run this from the command line: + +```bash +babel src --out-dir lib --source-maps +``` + +#### Using Babel with Webpack + +If you're using Webpack as a bundler, you can integrate Babel to Webpack, however this requires some additional dependencies, in addition to the steps above. + +```bash +npm install babel-core babel-loader --save-dev +``` + +Create a file called `.webpack.config` + +```javascript +module.exports = { + entry: './src/index.js', + output: { + path: './bin', + filename: 'app.js', + }, + module: { + loaders: [{ + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel-loader' + }] + } +} +``` + +--- + +### JSX vs hyperscript + +JSX is essentially a trade-off: it introduces a non-standard syntax that cannot be run without appropriate tooling, in order to allow a developer to write HTML code using curly braces. The main benefit of using JSX instead of regular HTML is that the JSX specification is much stricter and yields syntax errors when appropriate, whereas HTML is far too forgiving and makes syntax issues difficult to spot. + +Unlike HTML, JSX is case-sensitive. This means `
` is different from `
` (all lower case). The former compiles to `m("div", {className: "test"})` and the latter compiles to `m("div", {classname: "test"})`, which is not a valid way of creating a class attribute). Fortunately, Mithril supports standard HTML attribute names, and thus, this example can be written like regular HTML: `
`. + +JSX is useful for teams where HTML is primarily written by someone without Javascript experience, but it requires a significant amount of tooling to maintain (whereas plain HTML can, for the most part, simply be opened in a browser) + +Hyperscript is the compiled representation of JSX. It's designed to be readable and can also be used as-is, instead of JSX (as is done in most of the documentation). Hyperscript tends to be terser than JSX for a couple of reasons: + +1 - it does not require repeating the tag name in closing tags (e.g. `m("div")` vs `
`) +2 - static attributes can be written using CSS selector syntax (i.e. `m("a.button")` vs `
` + +In addition, since hyperscript is plain Javascript, it's often more natural to indent than JSX: + +``` +//JSX +var BigComponent = { + activate: function() {/*...*/}, + deactivate: function() {/*...*/}, + update: function() {/*...*/}, + view: function(vnode) { + return [ + {vnode.attrs.items.map(function(item) { + return
{item.name}
+ })} +
+ ] + } +} + +// hyperscript +var BigComponent = { + activate: function() {/*...*/}, + deactivate: function() {/*...*/}, + update: function() {/*...*/}, + view: function(vnode) { + return [ + vnode.attrs.items.map(function(item) { + return m("div", item.name) + }), + m("div", { + ondragover: this.activate, + ondragleave: this.deactivate, + ondragend: this.deactivate, + ondrop: this.update, + onblur: this.deactivate, + }) + ] + } +} +``` + +In non-trivial applications, it's possible for components to have more control flow and component configuration code than markup, making a Javascript-first approach more readable than an HTML-first approach. + +Needless to say, since hyperscript is pure Javascript, there's no need to run a compilation step to produce runnable code. + +--- + +### Converting HTML + +In Mithril, well-formed HTML is valid JSX. Little effort other than copy-pasting is required to integrate an independently produced HTML file into a project using JSX. + +When using hyperscript, it's necessary to convert HTML to hyperscript syntax before the code can be run. To facilitate this, you can [use the HTML-to-Mithril-template converter](http://arthurclemens.github.io/mithril-template-converter/index.html). From 4b626896fc62f5de0fb352d4dada72242e184044 Mon Sep 17 00:00:00 2001 From: Gandalf-the-Bot Date: Sat, 7 Jan 2017 20:20:45 +0000 Subject: [PATCH 6/7] Bundled output for commit 4fc9368a30b88b3eb485fa7110cf3e2d9c3093db [skip ci] --- README.md | 2 +- mithril.js | 2 +- mithril.min.js | 77 +++++++++++++++++++++++++------------------------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index b6e99098..bfe22f41 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,6 @@ There are over 4000 assertions in the test suite, and tests cover even difficult ## Modularity -Despite the huge improvements in performance and modularity, the new codebase is smaller than v0.2.x, currently clocking at 7.59 KB min+gzip +Despite the huge improvements in performance and modularity, the new codebase is smaller than v0.2.x, currently clocking at 7.60 KB min+gzip In addition, Mithril is now completely modular: you can import only the modules that you need and easily integrate 3rd party modules if you wish to use a different library for routing, ajax, and even rendering diff --git a/mithril.js b/mithril.js index 522c546d..cbc385e9 100644 --- a/mithril.js +++ b/mithril.js @@ -5,7 +5,7 @@ function Vnode(tag, key, attrs0, children, text, dom) { } Vnode.normalize = function(node) { if (Array.isArray(node)) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined) - if (node != null && typeof node !== "object") return Vnode("#", undefined, undefined, node, undefined, undefined) + if (node != null && typeof node !== "object") return Vnode("#", undefined, undefined, node === false ? "" : node, undefined, undefined) return node } Vnode.normalizeChildren = function normalizeChildren(children) { diff --git a/mithril.min.js b/mithril.min.js index ff483e40..ad515a32 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,41 +1,42 @@ new function(){function w(a,c,k,d,h,m){return{tag:a,key:c,attrs:k,children:d,text:h,dom:m,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function B(a){if(null==a||"string"!==typeof a&&null==a.view)throw Error("The selector must be either a string or a component.");if("string"===typeof a&&void 0===G[a]){for(var c,k,d=[],h={};c=N.exec(a);){var m=c[1],q=c[2];""===m&&""!==q?k=q:"#"===m?h.id=q:"."===m?d.push(q):"["===c[3][0]&&((m=c[6])&&(m=m.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")), "class"===c[4]?d.push(m):h[c[4]]=m||!0)}0b.indexOf("?")?"?":"&";b+=d+c}return b}function q(a){try{return""!==a?JSON.parse(a):null}catch(r){throw Error(a);}}function l(a){return a.responseText} -function t(a,c){if("function"===typeof a)if(Array.isArray(c))for(var b=0;bn.status||304===n.status)c(t(b.type,a));else{var g=Error(n.responseText),e;for(e in a)g[e]=a[e];d(g)}}catch(f){d(f)}};k&&null!=b.data?n.send(b.data):n.send()}); -return!0===b.background?r:z(r)},jsonp:function(b,l){var q=k();b=d(b,l);var z=new c(function(c,d){var k=b.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+p++,l=a.document.createElement("script");a[k]=function(d){l.parentNode.removeChild(l);c(t(b.type,d));delete a[k]};l.onerror=function(){l.parentNode.removeChild(l);d(Error("JSONP request failed"));delete a[k]};null==b.data&&(b.data={});b.url=h(b.url,b.data);b.data[b.callbackKey||"callback"]=k;l.src=m(b.url,b.data);a.document.documentElement.appendChild(l)}); -return!0===b.background?z:q(z)},setCompletionCallback:function(a){z=a}}}(window,x),M=function(a){function c(g,e,a,b,c,d,h){for(;a=u&&r>=n;){var y=e[u],v=a[n];if(y!==v||f)if(null==y)u++;else if(null==v)n++;else if(y.key===v.key)u++,n++,m(g,y,v,b,l(e,u,d),f,h),f&&y.tag===v.tag&&t(g,q(y),d);else if(y=e[p],y!==v||f)if(null==y)p--;else if(null==v)n++;else if(y.key===v.key)m(g,y,v,b,l(e,p+1,d),f,h),(f||n=u&&r>=n;){y=e[p];v=a[r];if(y!==v||f)if(null==y)p--;else{if(null!=v)if(y.key===v.key)m(g,y,v,b,l(e,p+1,d),f,h),f&&y.tag===v.tag&&t(g,q(y),d),null!=y.dom&&(d=y.dom),p--;else{if(!A){A=e;var y=p,C={},w;for(w=0;wb.indexOf("?")?"?":"&";b+=d+c}return b}function q(a){try{return""!==a?JSON.parse(a):null}catch(r){throw Error(a); +}}function l(a){return a.responseText}function t(a,c){if("function"===typeof a)if(Array.isArray(c))for(var b=0;bn.status||304===n.status)c(t(b.type,a));else{var g=Error(n.responseText),e;for(e in a)g[e]=a[e]; +d(g)}}catch(f){d(f)}};k&&null!=b.data?n.send(b.data):n.send()});return!0===b.background?r:z(r)},jsonp:function(b,l){var q=k();b=d(b,l);var z=new c(function(c,d){var k=b.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+p++,l=a.document.createElement("script");a[k]=function(d){l.parentNode.removeChild(l);c(t(b.type,d));delete a[k]};l.onerror=function(){l.parentNode.removeChild(l);d(Error("JSONP request failed"));delete a[k]};null==b.data&&(b.data={});b.url=h(b.url,b.data);b.data[b.callbackKey|| +"callback"]=k;l.src=m(b.url,b.data);a.document.documentElement.appendChild(l)});return!0===b.background?z:q(z)},setCompletionCallback:function(a){z=a}}}(window,x),M=function(a){function c(g,e,a,b,c,d,h){for(;a=u&&r>=n;){var y=e[u],v=a[n];if(y!==v||f)if(null==y)u++;else if(null==v)n++;else if(y.key===v.key)u++,n++,m(g,y,v,b,l(e,u,d),f,h),f&&y.tag===v.tag&&t(g,q(y),d);else if(y=e[p],y!==v||f)if(null==y)p--;else if(null==v)n++;else if(y.key===v.key)m(g,y,v,b,l(e, +p+1,d),f,h),(f||n=u&&r>=n;){y=e[p];v=a[r];if(y!==v||f)if(null==y)p--;else{if(null!=v)if(y.key===v.key)m(g,y,v,b,l(e,p+1,d),f,h),f&&y.tag===v.tag&&t(g,q(y),d),null!=y.dom&&(d=y.dom),p--;else{if(!A){A=e;var y=p,C={},w;for(w=0;w Date: Sat, 7 Jan 2017 17:56:29 -0500 Subject: [PATCH 7/7] fix typo --- docs/jsx.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/jsx.md b/docs/jsx.md index 819d8315..ee6d606d 100644 --- a/docs/jsx.md +++ b/docs/jsx.md @@ -44,7 +44,7 @@ Components can be used by using a convention of uppercasing the first letter of ```javascript m.mount(document.body, ) -// equivalent to m.mount(document.body, m(Component)) +// equivalent to m.mount(document.body, m(MyComponent)) ``` ---