assign custom methods to custom element prototype - fixes #1369

pull/1375/head
Rich Harris 6 years ago
parent 890da3b02a
commit ef39f00902

@ -113,8 +113,6 @@ export default function dom(
? 'svelte/shared.js' ? 'svelte/shared.js'
: options.shared || ''; : options.shared || '';
let prototypeBase = `${name}.prototype`;
const proto = sharedPath const proto = sharedPath
? `@proto` ? `@proto`
: deindent` : deindent`
@ -294,8 +292,9 @@ export default function dom(
`} `}
} }
customElements.define("${compiler.tag}", ${name}); @assign(${name}.prototype, ${proto});
@assign(@assign(${prototypeBase}, ${proto}), { ${templateProperties.methods && `@assign(${name}.prototype, %methods);`}
@assign(${name}.prototype, {
_mount(target, anchor) { _mount(target, anchor) {
target.insertBefore(this, anchor); target.insertBefore(this, anchor);
}, },
@ -304,6 +303,8 @@ export default function dom(
this.parentNode.removeChild(this); this.parentNode.removeChild(this);
} }
}); });
customElements.define("${compiler.tag}", ${name});
`); `);
} else { } else {
builder.addBlock(deindent` builder.addBlock(deindent`
@ -311,8 +312,8 @@ export default function dom(
${constructorBody} ${constructorBody}
} }
@assign(${prototypeBase}, ${proto}); @assign(${name}.prototype, ${proto});
${templateProperties.methods && `@assign(${prototypeBase}, %methods);`} ${templateProperties.methods && `@assign(${name}.prototype, %methods);`}
`); `);
} }

@ -0,0 +1,13 @@
<p>{foo}</p>
<script>
export default {
tag: 'custom-element',
methods: {
updateFoo(value) {
this.foo = value;
}
}
};
</script>

@ -0,0 +1,12 @@
import * as assert from 'assert';
import './main.html';
export default function (target) {
target.innerHTML = '<custom-element name="world"></custom-element>';
const el = target.querySelector('custom-element');
el.updateFoo(42);
const p = el.shadowRoot.querySelector('p');
assert.equal(p.textContent, '42');
}

@ -136,7 +136,7 @@ var proto = {
/* generated by Svelte vX.Y.Z */ /* generated by Svelte vX.Y.Z */
function link(node) { function link(node) {
function onClick(event) { function onClick(event) {
event.preventDefault(); event.preventDefault();
history.pushState(null, null, event.target.href); history.pushState(null, null, event.target.href);

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

@ -185,8 +185,8 @@ class SvelteComponent extends HTMLElement {
} }
} }
customElements.define("custom-element", SvelteComponent); assign(SvelteComponent.prototype, proto);
assign(assign(SvelteComponent.prototype, proto), { assign(SvelteComponent.prototype, {
_mount(target, anchor) { _mount(target, anchor) {
target.insertBefore(this, anchor); target.insertBefore(this, anchor);
}, },
@ -196,4 +196,6 @@ assign(assign(SvelteComponent.prototype, proto), {
} }
}); });
customElements.define("custom-element", SvelteComponent);
export default SvelteComponent; export default SvelteComponent;

@ -51,8 +51,8 @@ class SvelteComponent extends HTMLElement {
} }
} }
customElements.define("custom-element", SvelteComponent); assign(SvelteComponent.prototype, proto);
assign(assign(SvelteComponent.prototype, proto), { assign(SvelteComponent.prototype, {
_mount(target, anchor) { _mount(target, anchor) {
target.insertBefore(this, anchor); target.insertBefore(this, anchor);
}, },
@ -61,4 +61,6 @@ assign(assign(SvelteComponent.prototype, proto), {
this.parentNode.removeChild(this); this.parentNode.removeChild(this);
} }
}); });
customElements.define("custom-element", SvelteComponent);
export default SvelteComponent; export default SvelteComponent;
Loading…
Cancel
Save