diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index 19c538d486..e9f9665bb0 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -334,10 +334,10 @@ export default class Element extends Node { const indexName = block.indexNames.get(contextName); initialProps.push( - `${listName}: state.${listName},\n${indexName}: state.${indexName}` + `${listName}: ctx.${listName},\n${indexName}: ctx.${indexName}` ); updates.push( - `${name}._svelte.${listName} = state.${listName};\n${name}._svelte.${indexName} = state.${indexName};` + `${name}._svelte.${listName} = ctx.${listName};\n${name}._svelte.${indexName} = ctx.${indexName};` ); }); diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index c8e9cda42f..cc01b67e8b 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -181,6 +181,18 @@ export default function ssr( ` } + ${ + /__each/.test(generator.renderCode) && deindent` + function __each(items, assign, fn) { + let str = ''; + for (let i = 0; i < items.length; i += 1) { + str += fn(assign(items[i], i)); + } + return str; + } + ` + } + ${ /__isPromise/.test(generator.renderCode) && deindent` function __isPromise(value) { diff --git a/src/generators/server-side-rendering/visitors/EachBlock.ts b/src/generators/server-side-rendering/visitors/EachBlock.ts index 79907e2920..ed8bd2f123 100644 --- a/src/generators/server-side-rendering/visitors/EachBlock.ts +++ b/src/generators/server-side-rendering/visitors/EachBlock.ts @@ -10,7 +10,11 @@ export default function visitEachBlock( ) { const { snippet } = node.expression; - const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : `(${node.context})`} => \``; + const getContext = node.index + ? `(item, i) => Object.assign({}, ctx, { ${node.context}: item, ${node.index}: i })` + : `item => Object.assign({}, ctx, { ${node.context}: item })`; + + const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}__each(${snippet}, ${getContext}, ctx => \``; generator.append(open); // TODO should this be the generator's job? It's duplicated between @@ -36,7 +40,7 @@ export default function visitEachBlock( visit(generator, childBlock, child); }); - const close = `\`).join("")`; + const close = `\`)`; generator.append(close); if (node.else) {