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

@ -660,7 +660,7 @@ export default class Element extends Node {
snippet = attribute.metadata.snippet;
dependencies = attribute.metadata.dependencies;
}
const name = block.getUniqueName(
`${attribute.name.replace(/[^a-zA-Z0-9_$]/g, '_')}_action`
);
@ -669,22 +669,22 @@ 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) {
let conditional = `typeof ${name}.update === 'function' && `;
const deps = dependencies.map(dependency => `changed.${dependency}`).join(' || ');
conditional += dependencies.length > 1 ? `(${deps})` : deps;
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);
}
};
}

@ -2,7 +2,7 @@
import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";
function link(node) {
function onClick(event) {
event.preventDefault();
history.pushState(null, null, event.target.href);
@ -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