mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
support methods as actions (#5398)
This commit is contained in:
parent
b3f54bd2cf
commit
254096d320
@ -2,6 +2,7 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
* Support `use:obj.method` as actions ([#3935](https://github.com/sveltejs/svelte/issues/3935))
|
||||
* Support `_` as numeric separator ([#5407](https://github.com/sveltejs/svelte/issues/5407))
|
||||
* Fix assignments to properties on store values ([#5412](https://github.com/sveltejs/svelte/issues/5412))
|
||||
* Support `import.meta` in template expressions ([#5422](https://github.com/sveltejs/svelte/issues/5422))
|
||||
|
@ -26,11 +26,19 @@ export function add_action(block: Block, target: string, action: Action) {
|
||||
|
||||
block.add_variable(id);
|
||||
|
||||
const fn = block.renderer.reference(action.name);
|
||||
const [obj, ...properties] = action.name.split('.');
|
||||
|
||||
const fn = block.renderer.reference(obj);
|
||||
|
||||
if (properties.length) {
|
||||
block.event_listeners.push(
|
||||
x`@action_destroyer(${id} = ${fn}.${properties.join('.')}(${target}, ${snippet}))`
|
||||
);
|
||||
} else {
|
||||
block.event_listeners.push(
|
||||
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
|
||||
);
|
||||
}
|
||||
|
||||
if (dependencies && dependencies.length > 0) {
|
||||
let condition = x`${id} && @is_function(${id}.update)`;
|
||||
|
8
test/runtime/samples/action-object/_config.js
Normal file
8
test/runtime/samples/action-object/_config.js
Normal file
@ -0,0 +1,8 @@
|
||||
export default {
|
||||
html: `
|
||||
<button>action</button>
|
||||
`,
|
||||
async test({ assert, target, window }) {
|
||||
assert.equal(target.querySelector('button').foo, 'bar1337');
|
||||
}
|
||||
};
|
10
test/runtime/samples/action-object/main.svelte
Normal file
10
test/runtime/samples/action-object/main.svelte
Normal file
@ -0,0 +1,10 @@
|
||||
<script>
|
||||
const obj = {
|
||||
foo : "bar",
|
||||
action(element, { leet }) {
|
||||
element.foo = this.foo + leet;
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<button use:obj.action={{ leet: 1337 }}>action</button>
|
Loading…
Reference in New Issue
Block a user