feat($compiler): supports compiling v-bind to the weex native directive in recycle-list

This commit is contained in:
Hanks 2017-09-16 13:13:23 +08:00 committed by Evan You
parent c104cc582d
commit 8b893c13d6
5 changed files with 48 additions and 6 deletions

View File

@ -483,15 +483,22 @@ function genComponent (
})` })`
} }
function genProps (props: Array<{ name: string, value: string }>): string { function genProps (props: Array<{ name: string, value: any }>): string {
let res = '' let res = ''
for (let i = 0; i < props.length; i++) { for (let i = 0; i < props.length; i++) {
const prop = props[i] const prop = props[i]
res += `"${prop.name}":${transformSpecialNewlines(prop.value)},` res += `"${prop.name}":${generateValue(prop.value)},`
} }
return res.slice(0, -1) return res.slice(0, -1)
} }
function generateValue (value) {
if (typeof value === 'string') {
return transformSpecialNewlines(value)
}
return JSON.stringify(value)
}
// #3895, #4268 // #3895, #4268
function transformSpecialNewlines (text: string): string { function transformSpecialNewlines (text: string): string {
return text return text

View File

@ -20,7 +20,7 @@ export function addProp (el: ASTElement, name: string, value: string) {
(el.props || (el.props = [])).push({ name, value }) (el.props || (el.props = [])).push({ name, value })
} }
export function addAttr (el: ASTElement, name: string, value: string) { export function addAttr (el: ASTElement, name: string, value: any) {
(el.attrs || (el.attrs = [])).push({ name, value }) (el.attrs || (el.attrs = [])).push({ name, value })
} }

View File

@ -1,6 +1,7 @@
/* @flow */ /* @flow */
import { transformText } from './text' import { transformText } from './text'
import { transformVBind } from './v-bind'
let currentRecycleList = null let currentRecycleList = null
@ -22,6 +23,7 @@ function postTransformNode (el: ASTElement) {
if (el.tag === 'text') { if (el.tag === 'text') {
transformText(el) transformText(el)
} }
transformVBind(el)
} }
if (el === currentRecycleList) { if (el === currentRecycleList) {
currentRecycleList = null currentRecycleList = null

View File

@ -16,7 +16,9 @@ function genText (node: ASTNode) {
export function transformText (el: ASTElement) { export function transformText (el: ASTElement) {
// weex <text> can only contain text, so the parser // weex <text> can only contain text, so the parser
// always generates a single child. // always generates a single child.
addAttr(el, 'value', genText(el.children[0])) if (el.children.length) {
el.children = [] addAttr(el, 'value', genText(el.children[0]))
el.plain = false el.children = []
el.plain = false
}
} }

View File

@ -0,0 +1,31 @@
/* @flow */
import { getAndRemoveAttr, addAttr } from 'compiler/helpers'
function isBindingAttr (name) {
return /^(v\-bind)?\:/.test(name)
}
function parseRealName (name: string): string {
return name.replace(/^(v\-bind)?\:/, '')
}
export function transformVBind (el: ASTElement) {
if (!el.attrsList.length) {
return
}
el.attrsList.forEach(attr => {
// console.log('is binding attr:', attr.name, isBindingAttr(attr.name))
if (isBindingAttr(attr.name)) {
const realName: string = parseRealName(attr.name)
const binding = getAndRemoveAttr(el, attr.name)
if (el.attrs) {
el.attrs = el.attrs.filter(at => at.name !== realName) // omit duplicated
}
getAndRemoveAttr(el, realName)
addAttr(el, realName, { '@binding': binding })
}
})
el.hasBindings = false
// el.plain = true
}