diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 5e6669254acd..7133ce8eac22 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -83,6 +83,18 @@ export default class EachBlock extends Node { listNames }); + this.contextProps = [ + `${context}: ${listName}[#i]`, + `${indexName}: #i` + ]; + + if (this.destructuredContexts) { + for (let i = 0; i < this.destructuredContexts.length; i += 1) { + contexts.set(this.destructuredContexts[i], `${context}[${i}]`); + this.contextProps.push(`${this.destructuredContexts[i]}: ${listName}[#i][${i}]`); + } + } + this.generator.blocks.push(this.block); this.initChildren(this.block, stripWhitespace, nextSibling); block.addDependencies(this.block.dependencies); @@ -272,8 +284,7 @@ export default class EachBlock extends Node { for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { var ${key} = ${each_block_value}[#i].${this.key}; var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} })); if (${last}) ${last}.next = ${iteration}; @@ -379,8 +390,7 @@ export default class EachBlock extends Node { var ${iteration} = ${lookup}[${key}]; var ${this.each_context} = @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} }); ${dynamic && @@ -477,8 +487,7 @@ export default class EachBlock extends Node { for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { ${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} })); } `); @@ -576,8 +585,7 @@ export default class EachBlock extends Node { if (${condition}) { for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) { var ${this.each_context} = @assign({}, state, { - ${this.context}: ${each_block_value}[#i], - ${this.block.indexName}: #i + ${this.contextProps.join(',\n')} }); ${forLoopBody} diff --git a/src/generators/server-side-rendering/visitors/EachBlock.ts b/src/generators/server-side-rendering/visitors/EachBlock.ts index 59bd5fed0b6c..9a5ec300ca14 100644 --- a/src/generators/server-side-rendering/visitors/EachBlock.ts +++ b/src/generators/server-side-rendering/visitors/EachBlock.ts @@ -11,7 +11,9 @@ export default function visitEachBlock( block.contextualise(node.expression); const { dependencies, snippet } = node.metadata; - const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : node.context} => \``; + const context = node.destructuredContexts ? `[${node.destructuredContexts.join(', ')}]` : node.context; + + const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${context}, ${node.index})` : `(${context})`} => \``; generator.append(open); // TODO should this be the generator's job? It's duplicated between