From 7a8e17779ce43e951106ebfd4c628d77507bd7a4 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 24 Dec 2017 11:57:02 -0500 Subject: [PATCH] create initial data for dynamic components in correct place - fixes #1040 --- src/generators/nodes/Component.ts | 2 +- .../Green.html | 1 + .../Red.html | 1 + .../_config.js | 29 +++++++++++++++++++ .../main.html | 15 ++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/dynamic-component-bindings-recreated/Green.html create mode 100644 test/runtime/samples/dynamic-component-bindings-recreated/Red.html create mode 100644 test/runtime/samples/dynamic-component-bindings-recreated/_config.js create mode 100644 test/runtime/samples/dynamic-component-bindings-recreated/main.html diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 35100e16a1b3..1b9b1bb8b66b 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -256,13 +256,13 @@ export default class Component extends Node { var ${switch_vars.value} = ${snippet}; function ${switch_vars.props}(${params}) { + ${statements.length > 0 && statements.join('\n')} return { ${componentInitProperties.join(',\n')} }; } if (${switch_vars.value}) { - ${statements.length > 0 && statements.join('\n')} var ${name} = new ${expression}(${switch_vars.props}(${params})); ${beforecreate} diff --git a/test/runtime/samples/dynamic-component-bindings-recreated/Green.html b/test/runtime/samples/dynamic-component-bindings-recreated/Green.html new file mode 100644 index 000000000000..692847c1c204 --- /dev/null +++ b/test/runtime/samples/dynamic-component-bindings-recreated/Green.html @@ -0,0 +1 @@ +

green {{foo}}

\ No newline at end of file diff --git a/test/runtime/samples/dynamic-component-bindings-recreated/Red.html b/test/runtime/samples/dynamic-component-bindings-recreated/Red.html new file mode 100644 index 000000000000..8f10e3294dbe --- /dev/null +++ b/test/runtime/samples/dynamic-component-bindings-recreated/Red.html @@ -0,0 +1 @@ +

red {{foo}}

\ No newline at end of file diff --git a/test/runtime/samples/dynamic-component-bindings-recreated/_config.js b/test/runtime/samples/dynamic-component-bindings-recreated/_config.js new file mode 100644 index 000000000000..de3f0d0ebb1d --- /dev/null +++ b/test/runtime/samples/dynamic-component-bindings-recreated/_config.js @@ -0,0 +1,29 @@ +export default { + data: { + x: true, + foo: 'one' + }, + + html: ` +

green one

+ `, + + test(assert, component, target) { + component.set({ + x: false + }); + + assert.htmlEqual(target.innerHTML, ` +

red one

+ `); + + component.set({ + x: true, + foo: 'two' + }); + + assert.htmlEqual(target.innerHTML, ` +

green two

+ `); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/dynamic-component-bindings-recreated/main.html b/test/runtime/samples/dynamic-component-bindings-recreated/main.html new file mode 100644 index 000000000000..b9cdf9266099 --- /dev/null +++ b/test/runtime/samples/dynamic-component-bindings-recreated/main.html @@ -0,0 +1,15 @@ +<:Component {x ? Green : Red} bind:foo /> + + \ No newline at end of file