From c8747db57f1b4ed573020991dcb3d51260c5a0e1 Mon Sep 17 00:00:00 2001 From: Petter Ekrann Date: Wed, 28 Jun 2017 11:37:07 +0200 Subject: [PATCH 1/3] Moved the check to the top --- src/generators/dom/visitors/EachBlock.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/generators/dom/visitors/EachBlock.ts b/src/generators/dom/visitors/EachBlock.ts index 82a117eaae82..9b28ed558d24 100644 --- a/src/generators/dom/visitors/EachBlock.ts +++ b/src/generators/dom/visitors/EachBlock.ts @@ -33,7 +33,8 @@ export default function visitEachBlock( const { snippet } = block.contextualise(node.expression); - block.builders.init.addLine(`var ${each_block_value} = ${snippet};`); + //Warn user if "each_block_value" is null @rich_harris + block.builders.init.addLine(`var ${each_block_value} = ${snippet} || [];`); if (node.key) { keyed(generator, block, state, node, snippet, vars); From 98d93952bffc921cec8da474c59ab676e49c0590 Mon Sep 17 00:00:00 2001 From: Petter Ekrann Date: Wed, 28 Jun 2017 11:41:55 +0200 Subject: [PATCH 2/3] Added test for missing 'each' value --- .../each-block-missing-value/_config.js | 19 +++++++++++++++++++ .../each-block-missing-value/main.html | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 test/runtime/samples/each-block-missing-value/_config.js create mode 100644 test/runtime/samples/each-block-missing-value/main.html diff --git a/test/runtime/samples/each-block-missing-value/_config.js b/test/runtime/samples/each-block-missing-value/_config.js new file mode 100644 index 000000000000..13238ca9cb97 --- /dev/null +++ b/test/runtime/samples/each-block-missing-value/_config.js @@ -0,0 +1,19 @@ +export default { + data: { + noanimals: [ 'alpaca', 'baboon', 'capybara' ] + }, + + html: ` +

alpaca

+

baboon

+

capybara

+ `, + + test ( assert, component, target ) { + component.set({ noanimals: [ 'alpaca', 'baboon', 'caribou', 'dogfish' ] }); + assert.htmlEqual( target.innerHTML, `` ); + + component.set({ animals: [] }); + assert.htmlEqual( target.innerHTML, '' ); + } +}; diff --git a/test/runtime/samples/each-block-missing-value/main.html b/test/runtime/samples/each-block-missing-value/main.html new file mode 100644 index 000000000000..23bfc3c46565 --- /dev/null +++ b/test/runtime/samples/each-block-missing-value/main.html @@ -0,0 +1,3 @@ +{{#each animals as animal}} +

{{animal}}

+{{/each}} From 41aef1169fe22a13177b52984132be80b244d720 Mon Sep 17 00:00:00 2001 From: Petter Ekrann Date: Thu, 29 Jun 2017 09:23:18 +0200 Subject: [PATCH 3/3] Update EachBlock.ts --- src/generators/dom/visitors/EachBlock.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/generators/dom/visitors/EachBlock.ts b/src/generators/dom/visitors/EachBlock.ts index 9b28ed558d24..284500b737de 100644 --- a/src/generators/dom/visitors/EachBlock.ts +++ b/src/generators/dom/visitors/EachBlock.ts @@ -33,8 +33,9 @@ export default function visitEachBlock( const { snippet } = block.contextualise(node.expression); - //Warn user if "each_block_value" is null @rich_harris - block.builders.init.addLine(`var ${each_block_value} = ${snippet} || [];`); + + block.builders.init.addLine(`var ${each_block_value} = ${snippet};`); + block.builders.init.addLine(`if(!${each_block_value}) throw new Error('Missing field or value is null ${node.expression}');` if (node.key) { keyed(generator, block, state, node, snippet, vars);