Skip to content

Commit c9435fc

Browse files
authored
Merge pull request #1279 from jacwright/action-this
Make actions execute with the component context
2 parents 48643ca + 297ee65 commit c9435fc

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

src/generators/nodes/Element.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ export default class Element extends Node {
660660
snippet = attribute.metadata.snippet;
661661
dependencies = attribute.metadata.dependencies;
662662
}
663-
663+
664664
const name = block.getUniqueName(
665665
`${attribute.name.replace(/[^a-zA-Z0-9_$]/g, '_')}_action`
666666
);
@@ -669,22 +669,22 @@ export default class Element extends Node {
669669
const fn = `%actions-${attribute.name}`;
670670

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

675675
if (dependencies && dependencies.length) {
676676
let conditional = `typeof ${name}.update === 'function' && `;
677677
const deps = dependencies.map(dependency => `changed.${dependency}`).join(' || ');
678678
conditional += dependencies.length > 1 ? `(${deps})` : deps;
679-
679+
680680
block.builders.update.addConditional(
681681
conditional,
682-
`${name}.update(${snippet});`
682+
`${name}.update.call(#component, ${snippet});`
683683
);
684684
}
685685

686686
block.builders.destroy.addLine(
687-
`if (typeof ${name}.destroy === 'function') ${name}.destroy();`
687+
`if (typeof ${name}.destroy === 'function') ${name}.destroy.call(#component);`
688688
);
689689
});
690690
}

test/js/samples/action/expected-bundle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function create_main_fragment(component, state) {
202202

203203
h: function hydrate() {
204204
a.href = "#";
205-
link_action = link(a) || {};
205+
link_action = link.call(component, a) || {};
206206
},
207207

208208
m: function mount(target, anchor) {
@@ -216,7 +216,7 @@ function create_main_fragment(component, state) {
216216
},
217217

218218
d: function destroy$$1() {
219-
if (typeof link_action.destroy === 'function') link_action.destroy();
219+
if (typeof link_action.destroy === 'function') link_action.destroy.call(component);
220220
}
221221
};
222222
}

test/js/samples/action/expected.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
33

44
function link(node) {
5-
5+
66
function onClick(event) {
77
event.preventDefault();
88
history.pushState(null, null, event.target.href);
@@ -29,7 +29,7 @@ function create_main_fragment(component, state) {
2929

3030
h: function hydrate() {
3131
a.href = "#";
32-
link_action = link(a) || {};
32+
link_action = link.call(component, a) || {};
3333
},
3434

3535
m: function mount(target, anchor) {
@@ -43,7 +43,7 @@ function create_main_fragment(component, state) {
4343
},
4444

4545
d: function destroy() {
46-
if (typeof link_action.destroy === 'function') link_action.destroy();
46+
if (typeof link_action.destroy === 'function') link_action.destroy.call(component);
4747
}
4848
};
4949
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default {
2+
test ( assert, component, target, window ) {
3+
const button = target.querySelector( 'button' );
4+
const click = new window.MouseEvent( 'click' );
5+
6+
assert.htmlEqual( target.innerHTML, `<button>0</button>` );
7+
button.dispatchEvent( click );
8+
assert.htmlEqual( target.innerHTML, `<button>1</button>` );
9+
}
10+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<button use:foo>{{x}}</button>
2+
3+
<script>
4+
export default {
5+
actions: {
6+
foo(node) {
7+
let x = 0;
8+
9+
const handler = () => this.set({ x: x++ });
10+
11+
node.addEventListener('click', handler);
12+
handler();
13+
14+
return {
15+
destroy() {
16+
node.removeEventListener('click', handler);
17+
}
18+
};
19+
}
20+
},
21+
};
22+
</script>

0 commit comments

Comments
 (0)