Simplify vnode.state creation.

For components, `vnode.tag` is always an object.
This commit is contained in:
Pierre-Yves Gerardy 2016-08-02 17:59:16 +02:00
parent 8c5b5c7f30
commit 905309cb17
2 changed files with 6 additions and 14 deletions

View file

@ -72,7 +72,7 @@ Property | Type | Description
`text` | `(String|Number|Boolean)?` | This is used instead of `children` if a vnode contains a text node as its only child. This is done for performance reasons. Component vnodes never use the `text` property even if they have a text node as its only child.
`dom` | `Element?` | Points to the element that corresponds to the vnode. This property is `undefined` in the `oninit` lifecycle method. In fragment and trusted HTML vnodes, `dom` points to the first element in the range.
`domSize` | `Number?` | This is only set in fragment and trusted HTML vnodes, and it's `undefined` in all other vnode types. It defines the number of DOM elements that the vnode represents (starting from the element referenced by the `dom` property).
`state` | `Object` | An object that is persisted between redraws. In component vnodes, `state` is a deep clone of the component object.
`state` | `Object` | An object that is persisted between redraws. In component vnodes, `state` is a shallow clone of the component object.
`events` | `Object?` | An object that is persisted between redraws and that stores event handlers so that they can be removed using the DOM API. The `events` property is `undefined` if there are no event handlers defined. This property is only used internally by Mithril, do not use it.
---

View file

@ -91,7 +91,9 @@ module.exports = function($window) {
return element
}
function createComponent(vnode, hooks, ns) {
vnode.state = copy(vnode.tag)
// For object literals since `Vnode()` always sets the `state` field.
if (!vnode.state) vnode.state = {}
assign(vnode.state, vnode.tag)
initLifecycle(vnode.tag, vnode, hooks)
vnode.instance = Vnode.normalize(vnode.tag.view.call(vnode.state, vnode))
@ -500,18 +502,8 @@ module.exports = function($window) {
return false
}
function copy(data) {
if (data instanceof Array) {
var output = []
for (var i = 0; i < data.length; i++) output[i] = data[i]
return output
}
else if (typeof data === "object") {
var output = {}
for (var i in data) output[i] = data[i]
return output
}
return data
function assign(target, source) {
Object.keys(source).forEach(function(k){target[k] = source[k]})
}
function render(dom, vnodes) {