Преглед изворни кода

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

Hanks пре 8 година
родитељ
комит
8b893c13d6

+ 9 - 2
src/compiler/codegen/index.js

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

+ 1 - 1
src/compiler/helpers.js

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

+ 2 - 0
src/platforms/weex/compiler/modules/recycle-list/index.js

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

+ 5 - 3
src/platforms/weex/compiler/modules/recycle-list/text.js

@@ -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 - 0
src/platforms/weex/compiler/modules/recycle-list/v-bind.js

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