diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index 464ece1595..ba1b726870 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -360,7 +360,14 @@ export function processFor (el: ASTElement) { } } -export function parseFor (exp: string): ?Object { +type ForParseResult = { + for: string; + alias: string; + iterator1?: string; + iterator2?: string; +}; + +export function parseFor (exp: string): ?ForParseResult { const inMatch = exp.match(forAliasRE) if (!inMatch) return const res = {} diff --git a/src/platforms/weex/compiler/modules/recycle-list/index.js b/src/platforms/weex/compiler/modules/recycle-list/index.js index d163c2d854..1b85a45f99 100644 --- a/src/platforms/weex/compiler/modules/recycle-list/index.js +++ b/src/platforms/weex/compiler/modules/recycle-list/index.js @@ -1,5 +1,6 @@ /* @flow */ +import { preTransformRecycleList } from './recycle-list' import { postTransformComponent } from './component' import { postTransformComponentRoot } from './component-root' import { postTransformText } from './text' @@ -17,6 +18,7 @@ function shouldCompile (el: ASTElement, options: WeexCompilerOptions) { function preTransformNode (el: ASTElement, options: WeexCompilerOptions) { if (el.tag === 'recycle-list') { + preTransformRecycleList(el, options) currentRecycleList = el } if (shouldCompile(el, options)) { diff --git a/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js b/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js new file mode 100644 index 0000000000..22e3b5d552 --- /dev/null +++ b/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js @@ -0,0 +1,49 @@ +/* @flow */ + +import { parseFor } from 'compiler/parser/index' +import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers' + +/** + * Map the following syntax to corresponding attrs: + * + * + * ... + * ... + * + */ + +export function preTransformRecycleList ( + el: ASTElement, + options: WeexCompilerOptions +) { + const exp = getAndRemoveAttr(el, 'for') + if (!exp) { + if (options.warn) { + options.warn(`Invalid syntax: missing "for" expression.`) + } + return + } + + const res = parseFor(exp) + if (!res) { + if (options.warn) { + options.warn(`Invalid syntax: ${exp}.`) + } + return + } + + addRawAttr(el, ':list-data', res.for) + addRawAttr(el, 'alias', res.alias) + if (res.iterator2) { + // (item, key, index) for object iteration + // is this even supported? + addRawAttr(el, 'index', res.iterator2) + } else if (res.iterator1) { + addRawAttr(el, 'index', res.iterator1) + } + + const switchKey = getAndRemoveAttr(el, 'switch') + if (switchKey) { + addRawAttr(el, 'switch', switchKey) + } +} diff --git a/test/weex/cases/recycle-list/attrs.vdom.js b/test/weex/cases/recycle-list/attrs.vdom.js index 0d15ed8341..ccce367fc3 100644 --- a/test/weex/cases/recycle-list/attrs.vdom.js +++ b/test/weex/cases/recycle-list/attrs.vdom.js @@ -7,12 +7,12 @@ { type: 'A', count: 2, source: 'http://whatever.com/y.png' }, { type: 'A', count: 3, source: 'http://whatever.com/z.png' } ], - templateKey: 'type', + switch: 'type', alias: 'item' }, children: [{ type: 'cell-slot', - attr: { append: 'tree', templateType: 'A' }, + attr: { append: 'tree', case: 'A' }, children: [{ type: 'image', attr: { diff --git a/test/weex/cases/recycle-list/attrs.vue b/test/weex/cases/recycle-list/attrs.vue index b9de8f9e71..1e2d0318af 100644 --- a/test/weex/cases/recycle-list/attrs.vue +++ b/test/weex/cases/recycle-list/attrs.vue @@ -1,6 +1,6 @@