move flatten into vdom implementation

This commit is contained in:
Evan You 2016-04-10 23:54:53 -04:00
parent c1a8267c94
commit 69e94eb453
4 changed files with 21 additions and 34 deletions

View File

@ -86,7 +86,7 @@ function genChildren (el) {
if (!el.children.length) {
return 'undefined'
}
return '__flatten__([' + el.children.map(genNode).join(',') + '])'
return '[' + el.children.map(genNode).join(',') + ']'
}
function genNode (node) {

View File

@ -43,23 +43,6 @@ export default class Component {
: dynamic
}
__flatten__ (arr) {
var res = []
for (var i = 0, l = arr.length; i < l; i++) {
var e = arr[i]
if (Array.isArray(e)) {
for (var j = 0, k = e.length; j < k; j++) {
if (e[j]) {
res.push(e[j])
}
}
} else if (e) {
res.push(e)
}
}
return res
}
_proxy (key) {
if (!isReserved(key)) {
// need to store ref to self here

View File

@ -10,24 +10,29 @@ function addNS(data, children) {
}
}
export default function h (tag, b, c) {
var data = {}, children, text, i
if (arguments.length === 3) {
data = b
if (isArray(c)) { children = c }
else if (isPrimitive(c)) { text = c }
} else if (arguments.length === 2) {
if (isArray(b)) { children = b }
else if (isPrimitive(b)) { text = b }
else { data = b }
}
export default function h (tag, data, children) {
if (isArray(children)) {
for (i = 0; i < children.length; ++i) {
if (isPrimitive(children[i])) children[i] = VNode(undefined, undefined, undefined, children[i])
let _children = children
children = []
for (let i = 0, l = _children.length; i < l; i++) {
let e = _children[i]
// flatten nested
if (isArray(e)) {
for (let j = 0, k = e.length; j < k; j++) {
if (e[j]) {
children.push(e[j])
}
}
} else if (isPrimitive(e)) {
// convert primitive to vnode
children.push(VNode(undefined, undefined, undefined, e))
} else if (e) {
children.push(e)
}
}
}
if (tag === 'svg') {
addNS(data, children)
}
return VNode(tag, data, children, text, undefined)
return VNode(tag, data, children, undefined, undefined)
}

View File

@ -1,4 +1,3 @@
export default function VNode (sel, data, children, text, elm) {
const key = data === undefined ? undefined : data.key
return { sel, data, children, text, elm, key }
return { sel, data, children, text, elm, key: data && data.key }
}