From fa34941cee4251b3a5a5db9243acd5cd8a6581ce Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Wed, 25 May 2016 10:27:28 -0700 Subject: [PATCH 1/3] Starting on a migration guide I'm sure there's other pieces I'm forgetting --- docs/v1.x-migration.md | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 docs/v1.x-migration.md diff --git a/docs/v1.x-migration.md b/docs/v1.x-migration.md new file mode 100644 index 00000000..5f8a4f6a --- /dev/null +++ b/docs/v1.x-migration.md @@ -0,0 +1,67 @@ +Migrating from `v0.2.x` to `v1.x` +================================= + +`v1.x` is largely API-compatible with `v0.2.x`, but there are a few breaking changes. + +- [`config` function](#config-function) +- [Cancelling redraw from event handlers](#cancelling-redraw-from-event-handlers) + +## `config` function + +In `v0.2.x` mithril provided a single lifecycle method, `config`. `v1.x` provides much more fine-grained control over the lifecycle of a vnode. + +### `v0.2.x` + +```js +m("div", { + config : function(element, isInitialized) { + // runs on each redraw + // isInitialized is a boolean representing if the node has been added to the DOM + } +}); +``` + +### `v1.x` + +More documentation on these new methods is available in [lifecycle-methods.md](lifecycle-methods.md). + +```js +m("div", { + // Called before the DOM node is created + oninit : function(vnode) { ... }, + // Called after the DOM node is created + oncreate : function(vnode) { ... }, + // Called before the node is updated, return false to cancel + onbeforeupdate : function(vnode, old) { ... }, + // Called after the node is updated + onupdate : function(vnode) { ... }, + // Called before the node is removed, call done() when ready for the node to be removed from the DOM + onbeforeremove : function(vnode, done) { ... }, + // Called before the node is removed, but after onbeforeremove calls done() + onremove : function(vnode) { ... } +}); +``` + +## Cancelling redraw from event handlers + +`m.mount()` and `m.route()` still automatically redraw after a DOM event handler runs. Cancelling these redraws from within your event handlers is now done by setting the `redraw` property on the passed-in event object to `false`. + +### `v0.2.x` + +```js +m("div", { + onclick : function(e) { + m.redraw.strategy("none"); + } +}) +``` + +### `v1.x` + +```js +m("div", { + onclick : function(e) { + e.redraw = false; + } +}) +``` From 578ff6d970a30db32c0782f16a7866343a2a19cf Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Wed, 25 May 2016 23:30:49 +0200 Subject: [PATCH 2/3] Adding how you could access the DOM element in the new lifecycle methods --- docs/v1.x-migration.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/v1.x-migration.md b/docs/v1.x-migration.md index 5f8a4f6a..1e80cbe8 100644 --- a/docs/v1.x-migration.md +++ b/docs/v1.x-migration.md @@ -42,6 +42,8 @@ m("div", { }); ``` +If available the DOM-Element of the vnode can be accessed at `vnode.dom`. + ## Cancelling redraw from event handlers `m.mount()` and `m.route()` still automatically redraw after a DOM event handler runs. Cancelling these redraws from within your event handlers is now done by setting the `redraw` property on the passed-in event object to `false`. From 6a233cd03c7f227886e05ff459063b5b322bc6b1 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Wed, 25 May 2016 23:06:52 -0700 Subject: [PATCH 3/3] Add more migration notes (#1075) --- docs/v1.x-migration.md | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/docs/v1.x-migration.md b/docs/v1.x-migration.md index 1e80cbe8..54a047c5 100644 --- a/docs/v1.x-migration.md +++ b/docs/v1.x-migration.md @@ -5,6 +5,8 @@ Migrating from `v0.2.x` to `v1.x` - [`config` function](#config-function) - [Cancelling redraw from event handlers](#cancelling-redraw-from-event-handlers) +- [Component `controller` function](#component-controller-function) +- [Passing components to `m()`](#passing-components-to-m) ## `config` function @@ -67,3 +69,69 @@ m("div", { } }) ``` + +## Component `controller` function + +In `v1.x` there is no more `controller` property in components, use `oninit` instead. + +### `v0.2.x` + +```js +m.mount(document.body, { + controller : function() { + var ctrl = this; + + ctrl.fooga = 1; + }, + + view : function(ctrl) { + return m("p", ctrl.fooga); + } +}); +``` + +### `v1.x` + +```js +m.mount(document.body, { + oninit : function(vnode) { + vnode.state.fooga = 1; + }, + + view : function(vnode) { + return m("p", vnode.state.fooga); + } +}); + +// OR + +m.mount(document.body, { + oninit : function(vnode) { + var ctrl = this; // this is bound to vnode.state by default + + ctrl.fooga = 1; + }, + + view : function(vnode) { + var ctrl = this; // this is bound to vnode.state by default + + return m("p", ctrl.fooga); + } +}); +``` + +## Passing components to `m()` + +In `v0.2.x` you could pass components as the second argument of `m()` w/o any wrapping required. To help with consistency in `v1.x` they must always be wrapped with a `m()` invocation. + +### `v0.2.x` + +```js +m("div", ); +``` + +### `v1.x` + +```js +m("div", m()); +```