support methods as action

pull/5398/head
Tan Li Hau 5 years ago
parent 04498769b5
commit 6026312aee

@ -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, ...property] = action.name.split('.');
block.event_listeners.push(
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
);
const fn = block.renderer.reference(obj);
if (property.length) {
block.event_listeners.push(
x`@action_destroyer(${id} = ${fn}.${property.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