mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
support static data and refs on components
This commit is contained in:
parent
49ac172bef
commit
d27e99d239
@ -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();` );
|
||||
}
|
||||
|
@ -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. <textarea readonly>
|
||||
local.init.push( deindent`
|
||||
${local.name}.setAttribute( '${attribute.name}', true );
|
||||
` );
|
||||
local.data.push( `${attribute.name}: true` );
|
||||
}
|
||||
|
||||
else if ( attribute.value.length === 1 ) {
|
||||
const value = attribute.value[0];
|
||||
|
||||
let result = '';
|
||||
|
||||
if ( value.type === 'Text' ) {
|
||||
// static attributes
|
||||
result = JSON.stringify( value.data );
|
||||
|
||||
local.init.push( deindent`
|
||||
${local.name}.setAttribute( '${attribute.name}', ${result} );
|
||||
` );
|
||||
const result = isNaN( parseFloat( value.data ) ) ? JSON.stringify( value.data ) : value.data;
|
||||
local.data.push( `${attribute.name}: ${result}` );
|
||||
}
|
||||
|
||||
else {
|
||||
// dynamic – but potentially non-string – attributes
|
||||
generator.contextualise( value.expression );
|
||||
result = `[✂${value.expression.start}-${value.expression.end}✂]`;
|
||||
const result = `[✂${value.expression.start}-${value.expression.end}✂]`;
|
||||
|
||||
local.update.push( deindent`
|
||||
${local.name}.setAttribute( '${attribute.name}', ${result} );
|
||||
` );
|
||||
throw new Error( 'TODO dynamic data' );
|
||||
// local.update.push( deindent`
|
||||
// ${local.name}.setAttribute( '${attribute.name}', ${result} );
|
||||
// ` );
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
throw new Error( 'TODO dynamic data' );
|
||||
|
||||
const value = ( attribute.value[0].type === 'Text' ? '' : `"" + ` ) + (
|
||||
attribute.value.map( chunk => {
|
||||
if ( chunk.type === 'Text' ) {
|
||||
|
2
test/compiler/component-data-static/Widget.html
Normal file
2
test/compiler/component-data-static/Widget.html
Normal file
@ -0,0 +1,2 @@
|
||||
<p>foo: {{foo}}</p>
|
||||
<p>baz: {{baz}} ({{typeof baz}})</p>
|
3
test/compiler/component-data-static/_config.js
Normal file
3
test/compiler/component-data-static/_config.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default {
|
||||
html: '<div><p>foo: bar</p>\n<p>baz: 42 (number)</p></div>'
|
||||
};
|
11
test/compiler/component-data-static/main.html
Normal file
11
test/compiler/component-data-static/main.html
Normal file
@ -0,0 +1,11 @@
|
||||
<div>
|
||||
<Widget foo='bar' baz='42'/>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
import Widget from './Widget.html';
|
||||
|
||||
export default {
|
||||
components: { Widget }
|
||||
};
|
||||
</script>
|
1
test/compiler/component-ref/Widget.html
Normal file
1
test/compiler/component-ref/Widget.html
Normal file
@ -0,0 +1 @@
|
||||
<p>i am a widget</p>
|
10
test/compiler/component-ref/_config.js
Normal file
10
test/compiler/component-ref/_config.js
Normal file
@ -0,0 +1,10 @@
|
||||
import * as assert from 'assert';
|
||||
import Widget from './Widget.html';
|
||||
|
||||
export default {
|
||||
html: '<div><p>i am a widget</p></div>',
|
||||
test ( component ) {
|
||||
const widget = component.refs.widget;
|
||||
assert.ok( widget instanceof Widget );
|
||||
}
|
||||
};
|
11
test/compiler/component-ref/main.html
Normal file
11
test/compiler/component-ref/main.html
Normal file
@ -0,0 +1,11 @@
|
||||
<div>
|
||||
<Widget ref:widget/>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
import Widget from './Widget.html';
|
||||
|
||||
export default {
|
||||
components: { Widget }
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user