diff --git a/compiler/generate/index.js b/compiler/generate/index.js
index 75820de03a..d2b8e5311c 100644
--- a/compiler/generate/index.js
+++ b/compiler/generate/index.js
@@ -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 = {
diff --git a/compiler/parse/state/tag.js b/compiler/parse/state/tag.js
index 7332bccd66..1ac9ca6b62 100644
--- a/compiler/parse/state/tag.js
+++ b/compiler/parse/state/tag.js
@@ -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 {
diff --git a/test/compiler/refs/_config.js b/test/compiler/refs/_config.js
new file mode 100644
index 0000000000..4cab4660da
--- /dev/null
+++ b/test/compiler/refs/_config.js
@@ -0,0 +1,9 @@
+import * as assert from 'assert';
+
+export default {
+ html: '',
+ test ( component, target ) {
+ const canvas = target.querySelector( 'canvas' );
+ assert.equal( canvas, component.refs.foo );
+ }
+};
diff --git a/test/compiler/refs/main.svelte b/test/compiler/refs/main.svelte
new file mode 100644
index 0000000000..e5594a4af0
--- /dev/null
+++ b/test/compiler/refs/main.svelte
@@ -0,0 +1 @@
+
diff --git a/test/parser/refs/input.svelte b/test/parser/refs/input.svelte
new file mode 100644
index 0000000000..e5594a4af0
--- /dev/null
+++ b/test/parser/refs/input.svelte
@@ -0,0 +1 @@
+
diff --git a/test/parser/refs/output.json b/test/parser/refs/output.json
new file mode 100644
index 0000000000..c158de0848
--- /dev/null
+++ b/test/parser/refs/output.json
@@ -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
+}