diff --git a/src/generators/dom/preprocess.js b/src/generators/dom/preprocess.js index 85985851e4..d3c97d235a 100644 --- a/src/generators/dom/preprocess.js +++ b/src/generators/dom/preprocess.js @@ -79,6 +79,14 @@ const preprocessors = { preprocessChildren( generator, node._block, node.children ); block.addDependencies( node._block.dependencies ); + + if ( node.else ) { + node.else._block = block.child({ + name: generator.getUniqueName( `${node._block.name}_else` ) + }); + + preprocessChildren( generator, node.else._block, node.else.children ); + } }, Element: ( generator, block, node ) => { diff --git a/src/generators/dom/visitors/EachBlock.js b/src/generators/dom/visitors/EachBlock.js index 8ce11b11de..e352d2cf85 100644 --- a/src/generators/dom/visitors/EachBlock.js +++ b/src/generators/dom/visitors/EachBlock.js @@ -4,9 +4,7 @@ import visit from '../visit.js'; export default function visitEachBlock ( generator, block, state, node ) { const each_block = generator.getUniqueName( `each_block` ); - const each_block_else = generator.getUniqueName( `${each_block}_else` ); const create_each_block = node._block.name; - const create_each_block_else = generator.getUniqueName( `${create_each_block}_else` ); const each_block_value = node._block.listName; const iterations = block.getUniqueName( `${each_block}_iterations` ); const i = block.getUniqueName( `i` ); @@ -41,12 +39,14 @@ export default function visitEachBlock ( generator, block, state, node ) { `${generator.helper( 'destroyEach' )}( ${iterations}, ${isToplevel ? 'detach' : 'false'}, 0 );` ); if ( node.else ) { + const each_block_else = generator.getUniqueName( `${each_block}_else` ); + block.builders.create.addLine( `var ${each_block_else} = null;` ); // TODO neaten this up... will end up with an empty line in the block block.builders.create.addBlock( deindent` if ( !${each_block_value}.length ) { - ${each_block_else} = ${create_each_block_else}( ${params}, ${block.component} ); + ${each_block_else} = ${node.else._block.name}( ${params}, ${block.component} ); ${!isToplevel ? `${each_block_else}.mount( ${state.parentNode}, ${anchor} );` : ''} } ` ); @@ -61,7 +61,7 @@ export default function visitEachBlock ( generator, block, state, node ) { if ( !${each_block_value}.length && ${each_block_else} ) { ${each_block_else}.update( changed, ${params} ); } else if ( !${each_block_value}.length ) { - ${each_block_else} = ${create_each_block_else}( ${params}, ${block.component} ); + ${each_block_else} = ${node.else._block.name}( ${params}, ${block.component} ); ${each_block_else}.mount( ${anchor}.parentNode, ${anchor} ); } else if ( ${each_block_else} ) { ${each_block_else}.destroy( true ); @@ -89,15 +89,11 @@ export default function visitEachBlock ( generator, block, state, node ) { generator.addBlock( childBlock ); if ( node.else ) { - const childBlock = block.child({ - name: create_each_block_else - }); - node.else.children.forEach( child => { - visit( generator, childBlock, childState, child ); + visit( generator, node.else._block, childState, child ); }); - generator.addBlock( childBlock ); + generator.addBlock( node.else._block ); } }