use ctx in SSR

pull/1367/head
Rich Harris 7 years ago
parent c0d241d819
commit f8db2eee61

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

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

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

Loading…
Cancel
Save