fix each-else blocks that are empty on initial render (#662)

pull/663/head
Rich Harris 8 years ago
parent f6f929bded
commit dcb905aab6

@ -66,6 +66,7 @@ export default function visitEachBlock(
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
if ( !${each_block_value}.length ) { if ( !${each_block_value}.length ) {
${each_block_else} = ${node.else._block.name}( ${params}, ${block.component} ); ${each_block_else} = ${node.else._block.name}( ${params}, ${block.component} );
${each_block_else}.create();
} }
`); `);

@ -10,7 +10,7 @@ export default function visitEachBlock(
) { ) {
const { dependencies, snippet } = block.contextualise(node.expression); const { dependencies, snippet } = block.contextualise(node.expression);
const open = `\${ ${snippet}.map( ${node.index const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map( ${node.index
? `( ${node.context}, ${node.index} )` ? `( ${node.context}, ${node.index} )`
: node.context} => \``; : node.context} => \``;
generator.append(open); generator.append(open);
@ -36,6 +36,16 @@ export default function visitEachBlock(
visit(generator, childBlock, child); visit(generator, childBlock, child);
}); });
const close = `\` ).join( '' )}`; const close = `\` ).join( '' )`;
generator.append(close); generator.append(close);
if (node.else) {
generator.append(` : \``);
node.else.children.forEach((child: Node) => {
visit(generator, block, child);
});
generator.append(`\``);
}
generator.append('}');
} }

@ -0,0 +1,21 @@
export default {
data: {
animals: [],
foo: 'something else'
},
html: `
before
<p>no animals, but rather something else</p>
after
`,
test ( assert, component, target ) {
component.set({ animals: ['wombat'] });
assert.htmlEqual( target.innerHTML, `
before
<p>wombat</p>
after
` );
}
};

@ -0,0 +1,7 @@
before
{{#each animals as animal}}
<p>{{animal}}</p>
{{else}}
<p>no animals, but rather {{foo}}</p>
{{/each}}
after
Loading…
Cancel
Save