Merge pull request #1279 from jacwright/action-this

Make actions execute with the component context
pull/1283/head
Rich Harris 7 years ago committed by GitHub
commit c9435fc87f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -669,7 +669,7 @@ export default class Element extends Node {
const fn = `%actions-${attribute.name}`;
block.builders.hydrate.addLine(
`${name} = ${fn}(${this.var}${snippet ? `, ${snippet}` : ''}) || {};`
`${name} = ${fn}.call(#component, ${this.var}${snippet ? `, ${snippet}` : ''}) || {};`
);
if (dependencies && dependencies.length) {
@ -679,12 +679,12 @@ export default class Element extends Node {
block.builders.update.addConditional(
conditional,
`${name}.update(${snippet});`
`${name}.update.call(#component, ${snippet});`
);
}
block.builders.destroy.addLine(
`if (typeof ${name}.destroy === 'function') ${name}.destroy();`
`if (typeof ${name}.destroy === 'function') ${name}.destroy.call(#component);`
);
});
}

@ -202,7 +202,7 @@ function create_main_fragment(component, state) {
h: function hydrate() {
a.href = "#";
link_action = link(a) || {};
link_action = link.call(component, a) || {};
},
m: function mount(target, anchor) {
@ -216,7 +216,7 @@ function create_main_fragment(component, state) {
},
d: function destroy$$1() {
if (typeof link_action.destroy === 'function') link_action.destroy();
if (typeof link_action.destroy === 'function') link_action.destroy.call(component);
}
};
}

@ -29,7 +29,7 @@ function create_main_fragment(component, state) {
h: function hydrate() {
a.href = "#";
link_action = link(a) || {};
link_action = link.call(component, a) || {};
},
m: function mount(target, anchor) {
@ -43,7 +43,7 @@ function create_main_fragment(component, state) {
},
d: function destroy() {
if (typeof link_action.destroy === 'function') link_action.destroy();
if (typeof link_action.destroy === 'function') link_action.destroy.call(component);
}
};
}

@ -0,0 +1,10 @@
export default {
test ( assert, component, target, window ) {
const button = target.querySelector( 'button' );
const click = new window.MouseEvent( 'click' );
assert.htmlEqual( target.innerHTML, `<button>0</button>` );
button.dispatchEvent( click );
assert.htmlEqual( target.innerHTML, `<button>1</button>` );
}
};

@ -0,0 +1,22 @@
<button use:foo>{{x}}</button>
<script>
export default {
actions: {
foo(node) {
let x = 0;
const handler = () => this.set({ x: x++ });
node.addEventListener('click', handler);
handler();
return {
destroy() {
node.removeEventListener('click', handler);
}
};
}
},
};
</script>
Loading…
Cancel
Save