mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
component is context for custom event handlers
This commit is contained in:
parent
170f4210bc
commit
20bf76b578
@ -137,7 +137,7 @@ export default function addElementAttributes ( generator, node, local ) {
|
||||
|
||||
if ( attribute.name in generator.events ) {
|
||||
local.init.push( deindent`
|
||||
const ${handlerName} = template.events.${attribute.name}( ${local.name}, function ( event ) {
|
||||
const ${handlerName} = template.events.${attribute.name}.call( component, ${local.name}, function ( event ) {
|
||||
${handlerBody}
|
||||
});
|
||||
` );
|
||||
|
16
test/compiler/event-handler-custom-context/_config.js
Normal file
16
test/compiler/event-handler-custom-context/_config.js
Normal file
@ -0,0 +1,16 @@
|
||||
export default {
|
||||
html: '<button>???</button>',
|
||||
|
||||
test ( assert, component, target, window ) {
|
||||
const event = new window.MouseEvent( 'click', {
|
||||
clientX: 42,
|
||||
clientY: 42
|
||||
});
|
||||
|
||||
const button = target.querySelector( 'button' );
|
||||
|
||||
button.dispatchEvent( event );
|
||||
|
||||
assert.equal( target.innerHTML, '<button>42</button>' );
|
||||
}
|
||||
};
|
28
test/compiler/event-handler-custom-context/main.html
Normal file
28
test/compiler/event-handler-custom-context/main.html
Normal file
@ -0,0 +1,28 @@
|
||||
<button on:tap='set({ z: event.answer })'>{{z}}</button>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data: () => ({
|
||||
z: '???',
|
||||
answer: '42'
|
||||
}),
|
||||
|
||||
events: {
|
||||
tap ( node, callback ) {
|
||||
const clickHandler = event => {
|
||||
callback({
|
||||
answer: this.get( 'answer' )
|
||||
});
|
||||
};
|
||||
|
||||
node.addEventListener( 'click', clickHandler, false );
|
||||
|
||||
return {
|
||||
teardown () {
|
||||
node.addEventListener( 'click', clickHandler, false );
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user