From b03c024160708f1f5ca67e65fd700f789d0db103 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Thu, 30 Mar 2017 05:02:15 -0400 Subject: [PATCH] bind callback passed to custom events to the node context (#428) --- .../attributes/addElementAttributes.js | 2 +- .../_config.js | 15 +++++++++++ .../main.html | 25 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/generator/samples/event-handler-custom-node-context/_config.js create mode 100644 test/generator/samples/event-handler-custom-node-context/main.html diff --git a/src/generators/dom/visitors/attributes/addElementAttributes.js b/src/generators/dom/visitors/attributes/addElementAttributes.js index de0699e4e490..1f1132aa737b 100644 --- a/src/generators/dom/visitors/attributes/addElementAttributes.js +++ b/src/generators/dom/visitors/attributes/addElementAttributes.js @@ -184,7 +184,7 @@ export default function addElementAttributes ( generator, node, local ) { local.init.addBlock( deindent` var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${generator.current.component}, ${local.name}, function ( event ) { ${handlerBody} - }); + }.bind( ${local.name} ) ); ` ); generator.current.builders.teardown.addLine( deindent` diff --git a/test/generator/samples/event-handler-custom-node-context/_config.js b/test/generator/samples/event-handler-custom-node-context/_config.js new file mode 100644 index 000000000000..e2099f96bcc4 --- /dev/null +++ b/test/generator/samples/event-handler-custom-node-context/_config.js @@ -0,0 +1,15 @@ +export default { + 'skip-ssr': true, + + html: '', + + test ( assert, component, target, window ) { + const event = new window.MouseEvent( 'click' ); + + const button = target.querySelector( 'button' ); + + button.dispatchEvent( event ); + + assert.equal( target.innerHTML, '' ); + } +}; diff --git a/test/generator/samples/event-handler-custom-node-context/main.html b/test/generator/samples/event-handler-custom-node-context/main.html new file mode 100644 index 000000000000..916001d82c9b --- /dev/null +++ b/test/generator/samples/event-handler-custom-node-context/main.html @@ -0,0 +1,25 @@ + + +