diff --git a/src/generators/dom/visitors/attributes/addElementAttributes.js b/src/generators/dom/visitors/attributes/addElementAttributes.js index de0699e4e4..1f1132aa73 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 0000000000..e2099f96bc --- /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 0000000000..916001d82c --- /dev/null +++ b/test/generator/samples/event-handler-custom-node-context/main.html @@ -0,0 +1,25 @@ + + +