Skip to content

Commit

Permalink
Merge pull request #1279 from jacwright/action-this
Browse files Browse the repository at this point in the history
Make actions execute with the component context
  • Loading branch information
Rich-Harris committed Mar 27, 2018
2 parents 48643ca + 297ee65 commit c9435fc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
Expand All @@ -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);`
);
});
}
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/action/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/action/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
};
}
Expand Down
10 changes: 10 additions & 0 deletions test/runtime/samples/action-this/_config.js
Original file line number Diff line number Diff line change
@@ -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>` );
}
};
22 changes: 22 additions & 0 deletions test/runtime/samples/action-this/main.html
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit c9435fc

Please sign in to comment.