Skip to content

Commit 254096d

Browse files
authored
support methods as actions (#5398)
1 parent b3f54bd commit 254096d

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* Support `use:obj.method` as actions ([#3935](https://github.com/sveltejs/svelte/issues/3935))
56
* Support `_` as numeric separator ([#5407](https://github.com/sveltejs/svelte/issues/5407))
67
* Fix assignments to properties on store values ([#5412](https://github.com/sveltejs/svelte/issues/5412))
78
* Support `import.meta` in template expressions ([#5422](https://github.com/sveltejs/svelte/issues/5422))

src/compiler/compile/render_dom/wrappers/shared/add_actions.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ export function add_action(block: Block, target: string, action: Action) {
2626

2727
block.add_variable(id);
2828

29-
const fn = block.renderer.reference(action.name);
29+
const [obj, ...properties] = action.name.split('.');
3030

31-
block.event_listeners.push(
32-
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
33-
);
31+
const fn = block.renderer.reference(obj);
32+
33+
if (properties.length) {
34+
block.event_listeners.push(
35+
x`@action_destroyer(${id} = ${fn}.${properties.join('.')}(${target}, ${snippet}))`
36+
);
37+
} else {
38+
block.event_listeners.push(
39+
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
40+
);
41+
}
3442

3543
if (dependencies && dependencies.length > 0) {
3644
let condition = x`${id} && @is_function(${id}.update)`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
html: `
3+
<button>action</button>
4+
`,
5+
async test({ assert, target, window }) {
6+
assert.equal(target.querySelector('button').foo, 'bar1337');
7+
}
8+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
const obj = {
3+
foo : "bar",
4+
action(element, { leet }) {
5+
element.foo = this.foo + leet;
6+
},
7+
}
8+
</script>
9+
10+
<button use:obj.action={{ leet: 1337 }}>action</button>

0 commit comments

Comments
 (0)