Skip to content

Commit

Permalink
Merge pull request #429 from sveltejs/gh-428
Browse files Browse the repository at this point in the history
Bind callback passed to custom events to the node context
  • Loading branch information
Rich-Harris authored Apr 2, 2017
2 parents 132de5c + b03c024 commit 0d5d7ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
'skip-ssr': true,

html: '<button>10</button>',

test ( assert, component, target, window ) {
const event = new window.MouseEvent( 'click' );

const button = target.querySelector( 'button' );

button.dispatchEvent( event );

assert.equal( target.innerHTML, '<button>11</button>' );
}
};
25 changes: 25 additions & 0 deletions test/generator/samples/event-handler-custom-node-context/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<button on:tap='set({ z: z + 1 })'>{{z}}</button>

<script>
export default {
data: () => ({
z: 10
}),

events: {
tap ( node, callback ) {
const clickHandler = event => {
callback(event);
};

node.addEventListener( 'click', clickHandler, false );

return {
teardown () {
node.addEventListener( 'click', clickHandler, false );
}
};
}
}
};
</script>

0 comments on commit 0d5d7ff

Please sign in to comment.