Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ export default class Element extends Node {
snippet = attribute.metadata.snippet;
dependencies = attribute.metadata.dependencies;
}

const name = block.getUniqueName(
`${attribute.name.replace(/[^a-zA-Z0-9_$]/g, '_')}_action`
);
Expand All @@ -669,22 +669,22 @@ export default class Element extends Node {
const fn = `%actions-${attribute.name}`;

block.builders.hydrate.addLine(
`${name} = ${fn}(${this.var}${snippet ? `, ${snippet}` : ''}) || {};`
`${name} = ${fn}.call(#component, ${this.var}${snippet ? `, ${snippet}` : ''}) || {};`
);

if (dependencies && dependencies.length) {
let conditional = `typeof ${name}.update === 'function' && `;
const deps = dependencies.map(dependency => `changed.${dependency}`).join(' || ');
conditional += dependencies.length > 1 ? `(${deps})` : deps;

block.builders.update.addConditional(
conditional,
`${name}.update(${snippet});`
`${name}.update.call(#component, ${snippet});`
);
}

block.builders.destroy.addLine(
`if (typeof ${name}.destroy === 'function') ${name}.destroy();`
`if (typeof ${name}.destroy === 'function') ${name}.destroy.call(#component);`
);
});
}
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/action/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ var proto = {
/* generated by Svelte vX.Y.Z */

function link(node) {

function onClick(event) {
event.preventDefault();
history.pushState(null, null, event.target.href);
Expand All @@ -210,7 +210,7 @@ function create_main_fragment(component, state) {

h: function hydrate() {
a.href = "#";
link_action = link(a) || {};
link_action = link.call(component, a) || {};
},

m: function mount(target, anchor) {
Expand All @@ -224,7 +224,7 @@ function create_main_fragment(component, state) {
},

d: function destroy$$1() {
if (typeof link_action.destroy === 'function') link_action.destroy();
if (typeof link_action.destroy === 'function') link_action.destroy.call(component);
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/action/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";

function link(node) {

function onClick(event) {
event.preventDefault();
history.pushState(null, null, event.target.href);
Expand All @@ -29,7 +29,7 @@ function create_main_fragment(component, state) {

h: function hydrate() {
a.href = "#";
link_action = link(a) || {};
link_action = link.call(component, a) || {};
},

m: function mount(target, anchor) {
Expand All @@ -43,7 +43,7 @@ function create_main_fragment(component, state) {
},

d: function destroy() {
if (typeof link_action.destroy === 'function') link_action.destroy();
if (typeof link_action.destroy === 'function') link_action.destroy.call(component);
}
};
}
Expand Down
10 changes: 10 additions & 0 deletions test/runtime/samples/action-this/_config.js
Original file line number Diff line number Diff line change
@@ -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>` );
}
};
22 changes: 22 additions & 0 deletions test/runtime/samples/action-this/main.html
Original file line number Diff line number Diff line change
@@ -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>