support methods as actions (#5398)

pull/5434/head
Tan Li Hau 4 years ago committed by GitHub
parent b3f54bd2cf
commit 254096d320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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('.');
block.event_listeners.push(
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
);
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)`;

@ -0,0 +1,8 @@
export default {
html: `
<button>action</button>
`,
async test({ assert, target, window }) {
assert.equal(target.querySelector('button').foo, 'bar1337');
}
};

@ -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…
Cancel
Save