mirror of
https://github.com/sveltejs/svelte.git
synced 2024-11-29 16:36:44 +01:00
dynamic component data
This commit is contained in:
parent
d27e99d239
commit
2686508af3
@ -20,12 +20,12 @@ export default {
|
||||
if ( isComponent ) {
|
||||
addComponentAttributes( generator, node, local );
|
||||
|
||||
if ( local.data.length ) {
|
||||
if ( local.staticAttributes.length ) {
|
||||
local.init.unshift( deindent`
|
||||
var ${name} = new template.components.${node.name}({
|
||||
target: ${generator.current.target},
|
||||
data: {
|
||||
${local.data.join( ',\n' )}
|
||||
${local.staticAttributes.join( ',\n' )}
|
||||
}
|
||||
});
|
||||
` );
|
||||
@ -37,6 +37,14 @@ export default {
|
||||
` );
|
||||
}
|
||||
|
||||
if ( local.dynamicAttributes.length ) {
|
||||
local.update.push( deindent`
|
||||
${name}.set({
|
||||
${local.dynamicAttributes.join( ',\n' )}
|
||||
});
|
||||
` );
|
||||
}
|
||||
|
||||
local.teardown.push( `${name}.teardown();` );
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
import deindent from '../../utils/deindent.js';
|
||||
|
||||
export default function addComponentAttributes ( generator, node, local ) {
|
||||
const variables = [];
|
||||
local.data = [];
|
||||
local.staticAttributes = [];
|
||||
local.dynamicAttributes = [];
|
||||
|
||||
node.attributes.forEach( attribute => {
|
||||
if ( attribute.type === 'Attribute' ) {
|
||||
@ -17,24 +17,21 @@ export default function addComponentAttributes ( generator, node, local ) {
|
||||
if ( value.type === 'Text' ) {
|
||||
// static attributes
|
||||
const result = isNaN( parseFloat( value.data ) ) ? JSON.stringify( value.data ) : value.data;
|
||||
local.data.push( `${attribute.name}: ${result}` );
|
||||
local.staticAttributes.push( `${attribute.name}: ${result}` );
|
||||
}
|
||||
|
||||
else {
|
||||
// dynamic – but potentially non-string – attributes
|
||||
// simple dynamic attributes
|
||||
generator.contextualise( value.expression );
|
||||
const result = `[✂${value.expression.start}-${value.expression.end}✂]`;
|
||||
|
||||
throw new Error( 'TODO dynamic data' );
|
||||
// local.update.push( deindent`
|
||||
// ${local.name}.setAttribute( '${attribute.name}', ${result} );
|
||||
// ` );
|
||||
// TODO only update attributes that have changed
|
||||
local.dynamicAttributes.push( `${attribute.name}: ${result}` );
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
throw new Error( 'TODO dynamic data' );
|
||||
|
||||
// complex dynamic attributes
|
||||
const value = ( attribute.value[0].type === 'Text' ? '' : `"" + ` ) + (
|
||||
attribute.value.map( chunk => {
|
||||
if ( chunk.type === 'Text' ) {
|
||||
@ -48,9 +45,8 @@ export default function addComponentAttributes ( generator, node, local ) {
|
||||
}).join( ' + ' )
|
||||
);
|
||||
|
||||
local.update.push( deindent`
|
||||
${local.name}.setAttribute( '${attribute.name}', ${value} );
|
||||
` );
|
||||
// TODO only update attributes that have changed
|
||||
local.dynamicAttributes.push( `${attribute.name}: ${value}` );
|
||||
}
|
||||
}
|
||||
|
||||
|
3
test/compiler/component-data-dynamic/Widget.html
Normal file
3
test/compiler/component-data-dynamic/Widget.html
Normal file
@ -0,0 +1,3 @@
|
||||
<p>foo: {{foo}}</p>
|
||||
<p>baz: {{baz}} ({{typeof baz}})</p>
|
||||
<p>qux: {{qux}}</p>
|
19
test/compiler/component-data-dynamic/_config.js
Normal file
19
test/compiler/component-data-dynamic/_config.js
Normal file
@ -0,0 +1,19 @@
|
||||
import * as assert from 'assert';
|
||||
|
||||
export default {
|
||||
data: {
|
||||
bar: 'lol',
|
||||
x: 2,
|
||||
compound: 'piece of'
|
||||
},
|
||||
html: `<div><p>foo: lol</p>\n<p>baz: 42 (number)</p>\n<p>qux: this is a piece of string</p></div>`,
|
||||
test ( component, target ) {
|
||||
component.set({
|
||||
bar: 'wut',
|
||||
x: 3,
|
||||
compound: 'rather boring'
|
||||
});
|
||||
|
||||
assert.equal( target.innerHTML, `<div><p>foo: wut</p>\n<p>baz: 43 (number)</p>\n<p>qux: this is a rather boring string</p></div>` );
|
||||
}
|
||||
};
|
11
test/compiler/component-data-dynamic/main.html
Normal file
11
test/compiler/component-data-dynamic/main.html
Normal file
@ -0,0 +1,11 @@
|
||||
<div>
|
||||
<Widget foo='{{bar}}' baz='{{40 + x}}' qux='this is a {{compound}} string'/>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
import Widget from './Widget.html';
|
||||
|
||||
export default {
|
||||
components: { Widget }
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user