From 2131b2863014a929d108577deb692308130ca134 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 8 Jul 2017 10:46:04 -0400 Subject: [PATCH] alias #component in hoisted event handlers - fixes #699 --- .../dom/visitors/Element/EventHandler.ts | 2 +- .../samples/event-handler-hoisted/_config.js | 18 ++++++++++++++++++ .../samples/event-handler-hoisted/main.html | 13 +++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/event-handler-hoisted/_config.js create mode 100644 test/runtime/samples/event-handler-hoisted/main.html diff --git a/src/generators/dom/visitors/Element/EventHandler.ts b/src/generators/dom/visitors/Element/EventHandler.ts index b66f6b8c3d..eb96c4e636 100644 --- a/src/generators/dom/visitors/Element/EventHandler.ts +++ b/src/generators/dom/visitors/Element/EventHandler.ts @@ -45,7 +45,7 @@ export default function visitEventHandler( const declarations = usedContexts.map(name => { if (name === 'state') { if (shouldHoist) state.usesComponent = true; - return `var state = #component.get();`; + return `var state = ${block.alias('component')}.get();`; } const listName = block.listNames.get(name); diff --git a/test/runtime/samples/event-handler-hoisted/_config.js b/test/runtime/samples/event-handler-hoisted/_config.js new file mode 100644 index 0000000000..3a949e5fad --- /dev/null +++ b/test/runtime/samples/event-handler-hoisted/_config.js @@ -0,0 +1,18 @@ +export default { + data: { + foo: [1], + a: 42 + }, + + html: ` + + `, + + test (assert, component, target, window) { + const button = target.querySelector('button'); + const event = new window.MouseEvent('click'); + + button.dispatchEvent(event); + assert.equal(component.snapshot, 42); + } +}; diff --git a/test/runtime/samples/event-handler-hoisted/main.html b/test/runtime/samples/event-handler-hoisted/main.html new file mode 100644 index 0000000000..527218d170 --- /dev/null +++ b/test/runtime/samples/event-handler-hoisted/main.html @@ -0,0 +1,13 @@ +{{#each foo as bar}} + +{{/each}} + + \ No newline at end of file