mirror of https://github.com/sveltejs/svelte
Merge pull request #1279 from jacwright/action-this
Make actions execute with the component contextpull/1283/head
commit
c9435fc87f
@ -0,0 +1,10 @@
|
||||
export default {
|
||||
test ( assert, component, target, window ) {
|
||||
const button = target.querySelector( 'button' );
|
||||
const click = new window.MouseEvent( 'click' );
|
||||
|
||||
assert.htmlEqual( target.innerHTML, `<button>0</button>` );
|
||||
button.dispatchEvent( click );
|
||||
assert.htmlEqual( target.innerHTML, `<button>1</button>` );
|
||||
}
|
||||
};
|
@ -0,0 +1,22 @@
|
||||
<button use:foo>{{x}}</button>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
actions: {
|
||||
foo(node) {
|
||||
let x = 0;
|
||||
|
||||
const handler = () => this.set({ x: x++ });
|
||||
|
||||
node.addEventListener('click', handler);
|
||||
handler();
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
node.removeEventListener('click', handler);
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
</script>
|
Loading…
Reference in new issue