Merge remote-tracking branch 'origin/rewrite' into rewrite

This commit is contained in:
Leo Horie 2017-01-08 08:21:46 -05:00
commit f0d6b0d58b
13 changed files with 437 additions and 65 deletions

120
docs/autoredraw.md Normal file
View file

@ -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)
```

69
docs/es6.md Normal file
View file

@ -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.

View file

@ -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
@ -9,6 +11,7 @@
- [Components](components.md)
- [Lifecycle methods](lifecycle-methods.md)
- [Keys](keys.md)
- [Autoredraw system](autoredraw.md)
- Social
- [Mithril Jobs](https://github.com/lhorie/mithril.js/wiki/JOBS)
- [How to contribute](contributing.md)

View file

@ -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 (including routing and XHR) 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
<body></body>
@ -91,7 +91,7 @@ m("main", [
])
```
Note: If you prefer `<html>` syntax, [it's possible to use it via a Babel plugin](https://babeljs.io/docs/plugins/transform-react-jsx/).
Note: If you prefer `<html>` 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.

177
docs/jsx.md Normal file
View file

@ -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 (
<main>
<h1>Hello world</h1>
</main>
)
}
}
```
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 = <a href={url}>{greeting + "!"}</a>
```
Components can be used by using a convention of uppercasing the first letter of the component name:
```javascript
m.mount(document.body, <MyComponent />)
// equivalent to m.mount(document.body, m(MyComponent))
```
---
### 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 `<div className="test"></div>` is different from `<div classname="test"></div>` (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: `<div class="test"></div>`.
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 `<div></div>`)
2 - static attributes can be written using CSS selector syntax (i.e. `m("a.button")` vs `<div class="button"></div>`
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 <div>{item.name}</div>
})}
<div
ondragover={this.activate}
ondragleave={this.deactivate}
ondragend={this.deactivate}
ondrop={this.update}
onblur={this.deactivate}
></div>
]
}
}
// 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).