mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
refs
This commit is contained in:
parent
ce79d3eca5
commit
44a2fd31f6
@ -94,6 +94,8 @@ export default function generate ( parsed, template ) {
|
||||
parent: null
|
||||
};
|
||||
|
||||
let usesRefs = false;
|
||||
|
||||
parsed.html.children.forEach( child => {
|
||||
walkHtml( child, {
|
||||
Comment: {
|
||||
@ -256,6 +258,14 @@ export default function generate ( parsed, template ) {
|
||||
createBinding( node, name, attribute, current, initStatements, updateStatements, teardownStatements, allUsedContexts );
|
||||
}
|
||||
|
||||
else if ( attribute.type === 'Ref' ) {
|
||||
usesRefs = true;
|
||||
|
||||
initStatements.push( deindent`
|
||||
component.refs.${attribute.name} = ${name};
|
||||
` );
|
||||
}
|
||||
|
||||
else {
|
||||
throw new Error( `Not implemented: ${attribute.type}` );
|
||||
}
|
||||
@ -580,7 +590,7 @@ export default function generate ( parsed, template ) {
|
||||
${renderers.reverse().join( '\n\n' )}
|
||||
|
||||
export default function createComponent ( options ) {
|
||||
var component = ${templateProperties.methods ? `Object.create( template.methods )` : `{}`};
|
||||
var component = ${templateProperties.methods ? `Object.create( template.methods )` : `{}`};${usesRefs ? `\ncomponent.refs = {}` : ``}
|
||||
var state = {};
|
||||
|
||||
var observers = {
|
||||
|
@ -149,6 +149,15 @@ function readAttribute ( parser ) {
|
||||
return readBindingDirective( parser, start, name.slice( 5 ) );
|
||||
}
|
||||
|
||||
if ( /^ref:/.test( name ) ) {
|
||||
return {
|
||||
start,
|
||||
end: parser.index,
|
||||
type: 'Ref',
|
||||
name: name.slice( 4 )
|
||||
};
|
||||
}
|
||||
|
||||
const value = parser.eat( '=' ) ? readAttributeValue( parser ) : true;
|
||||
|
||||
return {
|
||||
|
9
test/compiler/refs/_config.js
Normal file
9
test/compiler/refs/_config.js
Normal file
@ -0,0 +1,9 @@
|
||||
import * as assert from 'assert';
|
||||
|
||||
export default {
|
||||
html: '<canvas></canvas>',
|
||||
test ( component, target ) {
|
||||
const canvas = target.querySelector( 'canvas' );
|
||||
assert.equal( canvas, component.refs.foo );
|
||||
}
|
||||
};
|
1
test/compiler/refs/main.svelte
Normal file
1
test/compiler/refs/main.svelte
Normal file
@ -0,0 +1 @@
|
||||
<canvas ref:foo></canvas>
|
1
test/parser/refs/input.svelte
Normal file
1
test/parser/refs/input.svelte
Normal file
@ -0,0 +1 @@
|
||||
<canvas ref:foo></canvas>
|
26
test/parser/refs/output.json
Normal file
26
test/parser/refs/output.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"html": {
|
||||
"start": 0,
|
||||
"end": 25,
|
||||
"type": "Fragment",
|
||||
"children": [
|
||||
{
|
||||
"start": 0,
|
||||
"end": 25,
|
||||
"type": "Element",
|
||||
"name": "canvas",
|
||||
"attributes": [
|
||||
{
|
||||
"start": 8,
|
||||
"end": 15,
|
||||
"type": "Ref",
|
||||
"name": "foo"
|
||||
}
|
||||
],
|
||||
"children": []
|
||||
}
|
||||
]
|
||||
},
|
||||
"css": null,
|
||||
"js": null
|
||||
}
|
Loading…
Reference in New Issue
Block a user