0
0
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:
Rich-Harris 2016-11-23 13:20:02 -05:00
parent 170f4210bc
commit 20bf76b578
3 changed files with 45 additions and 1 deletions

View File

@ -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}
});
` );

View 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>' );
}
};

View 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>