Skip to content

Commit

Permalink
bind callback passed to custom events to the node context (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Mar 30, 2017
1 parent 38ee4f1 commit d017035
Show file tree
Hide file tree
Showing 3 changed files with 42 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,16 @@
export default {
solo: true,
'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 d017035

Please sign in to comment.