Skip to content

Commit

Permalink
feat($compiler): supports compiling v-bind to the weex native directi…
Browse files Browse the repository at this point in the history
…ve in recycle-list
  • Loading branch information
Hanks10100 authored and yyx990803 committed Dec 19, 2017
1 parent c104cc5 commit 8b893c1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/compiler/codegen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
for (let i = 0; i < props.length; i++) {
const prop = props[i]
res += `"${prop.name}":${transformSpecialNewlines(prop.value)},`
res += `"${prop.name}":${generateValue(prop.value)},`
}
return res.slice(0, -1)
}

function generateValue (value) {
if (typeof value === 'string') {
return transformSpecialNewlines(value)
}
return JSON.stringify(value)
}

// #3895, #4268
function transformSpecialNewlines (text: string): string {
return text
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function addProp (el: ASTElement, name: string, value: string) {
(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 })
}

Expand Down
2 changes: 2 additions & 0 deletions src/platforms/weex/compiler/modules/recycle-list/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { transformText } from './text'
import { transformVBind } from './v-bind'

let currentRecycleList = null

Expand All @@ -22,6 +23,7 @@ function postTransformNode (el: ASTElement) {
if (el.tag === 'text') {
transformText(el)
}
transformVBind(el)
}
if (el === currentRecycleList) {
currentRecycleList = null
Expand Down
8 changes: 5 additions & 3 deletions src/platforms/weex/compiler/modules/recycle-list/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function genText (node: ASTNode) {
export function transformText (el: ASTElement) {
// weex <text> can only contain text, so the parser
// always generates a single child.
addAttr(el, 'value', genText(el.children[0]))
el.children = []
el.plain = false
if (el.children.length) {
addAttr(el, 'value', genText(el.children[0]))
el.children = []
el.plain = false
}
}
31 changes: 31 additions & 0 deletions src/platforms/weex/compiler/modules/recycle-list/v-bind.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8b893c1

Please sign in to comment.