From d27e99d23939fa6a9394db3e0d5ffda6062edcae Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 21 Nov 2016 15:25:16 -0500 Subject: [PATCH] support static data and refs on components --- compiler/generate/visitors/Element.js | 21 +++++++++++---- .../attributes/addComponentAttributes.js | 27 +++++++++---------- .../component-data-static/Widget.html | 2 ++ .../compiler/component-data-static/_config.js | 3 +++ test/compiler/component-data-static/main.html | 11 ++++++++ test/compiler/component-ref/Widget.html | 1 + test/compiler/component-ref/_config.js | 10 +++++++ test/compiler/component-ref/main.html | 11 ++++++++ 8 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 test/compiler/component-data-static/Widget.html create mode 100644 test/compiler/component-data-static/_config.js create mode 100644 test/compiler/component-data-static/main.html create mode 100644 test/compiler/component-ref/Widget.html create mode 100644 test/compiler/component-ref/_config.js create mode 100644 test/compiler/component-ref/main.html diff --git a/compiler/generate/visitors/Element.js b/compiler/generate/visitors/Element.js index 1d4757f7f5..8cda9e7caa 100644 --- a/compiler/generate/visitors/Element.js +++ b/compiler/generate/visitors/Element.js @@ -20,11 +20,22 @@ export default { if ( isComponent ) { addComponentAttributes( generator, node, local ); - local.init.unshift( deindent` - var ${name} = new template.components.${node.name}({ - target: ${generator.current.target} - }); - ` ); + if ( local.data.length ) { + local.init.unshift( deindent` + var ${name} = new template.components.${node.name}({ + target: ${generator.current.target}, + data: { + ${local.data.join( ',\n' )} + } + }); + ` ); + } else { + local.init.unshift( deindent` + var ${name} = new template.components.${node.name}({ + target: ${generator.current.target} + }); + ` ); + } local.teardown.push( `${name}.teardown();` ); } diff --git a/compiler/generate/visitors/attributes/addComponentAttributes.js b/compiler/generate/visitors/attributes/addComponentAttributes.js index 55e37c4d9e..6e4e065dc3 100644 --- a/compiler/generate/visitors/attributes/addComponentAttributes.js +++ b/compiler/generate/visitors/attributes/addComponentAttributes.js @@ -1,41 +1,40 @@ import deindent from '../../utils/deindent.js'; export default function addComponentAttributes ( generator, node, local ) { + const variables = []; + local.data = []; + node.attributes.forEach( attribute => { if ( attribute.type === 'Attribute' ) { if ( attribute.value === true ) { // attributes without values, e.g.