From f30162f5e6ad774bf63ed151d4276d285f4cf913 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 1 May 2017 15:33:00 -0400 Subject: [PATCH] unkeyed each-blocks with outros --- src/generators/dom/visitors/EachBlock.js | 22 ++++++++++++++++--- .../transition-js-each-block-outro/_config.js | 16 ++++++++++++++ .../transition-js-each-block-outro/main.html | 18 +++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/runtime/samples/transition-js-each-block-outro/_config.js create mode 100644 test/runtime/samples/transition-js-each-block-outro/main.html diff --git a/src/generators/dom/visitors/EachBlock.js b/src/generators/dom/visitors/EachBlock.js index 3d637bc5d9..da3353f577 100644 --- a/src/generators/dom/visitors/EachBlock.js +++ b/src/generators/dom/visitors/EachBlock.js @@ -237,6 +237,24 @@ function unkeyed ( generator, block, state, node, snippet, { create_each_block, const start = node._block.hasUpdateMethod ? '0' : `${iterations}.length`; + const destroy = node._block.hasOutroMethod ? + deindent` + function outro ( i ) { + if ( ${iterations}[i] ) { + ${iterations}[i].outro( function () { + ${iterations}[i].destroy( true ); + ${iterations}[i] = null; + }); + } + } + + for ( ; ${i} < ${iterations}.length; ${i} += 1 ) outro( ${i} ); + ` : + deindent` + ${generator.helper( 'destroyEach' )}( ${iterations}, true, ${each_block_value}.length ); + ${iterations}.length = ${each_block_value}.length; + `; + block.builders.update.addBlock( deindent` var ${each_block_value} = ${snippet}; @@ -245,9 +263,7 @@ function unkeyed ( generator, block, state, node, snippet, { create_each_block, ${forLoopBody} } - ${generator.helper( 'destroyEach' )}( ${iterations}, true, ${each_block_value}.length ); - - ${iterations}.length = ${each_block_value}.length; + ${destroy} } ` ); } diff --git a/test/runtime/samples/transition-js-each-block-outro/_config.js b/test/runtime/samples/transition-js-each-block-outro/_config.js new file mode 100644 index 0000000000..5e8b99d88d --- /dev/null +++ b/test/runtime/samples/transition-js-each-block-outro/_config.js @@ -0,0 +1,16 @@ +export default { + data: { + things: [ 'a', 'b', 'c' ] + }, + + test ( assert, component, target, window, raf ) { + const divs = target.querySelectorAll( 'div' ); + + component.set({ things: [ 'a' ] }); + + raf.tick( 50 ); + assert.equal( divs[0].foo, undefined ); + assert.equal( divs[1].foo, 0.5 ); + assert.equal( divs[2].foo, 0.5 ); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/transition-js-each-block-outro/main.html b/test/runtime/samples/transition-js-each-block-outro/main.html new file mode 100644 index 0000000000..494132c9be --- /dev/null +++ b/test/runtime/samples/transition-js-each-block-outro/main.html @@ -0,0 +1,18 @@ +{{#each things as thing}} +
{{thing}}
+{{/each}} + + \ No newline at end of file