Browse Source

move flatten into vdom implementation

dev
Evan You 9 years ago
parent
commit
69e94eb453
  1. 2
      src/compiler/codegen.js
  2. 17
      src/instance/index.js
  3. 33
      src/vdom/h.js
  4. 3
      src/vdom/vnode.js

2
src/compiler/codegen.js

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

17
src/instance/index.js

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

33
src/vdom/h.js

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

3
src/vdom/vnode.js

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

Loading…
Cancel
Save