resolve #2837 added dynamic event bindings

pull/6876/head
Tom Shaw 5 years ago
parent b8efbb4cca
commit e81281c3f2

@ -237,6 +237,13 @@ if (typeof HTMLElement === 'function') {
export class SvelteComponent { export class SvelteComponent {
$$: T$$; $$: T$$;
$$set?: ($$props: any) => void; $$set?: ($$props: any) => void;
eventBindings: any;
constructor(props) {
if (props && props.props && Object.prototype.hasOwnProperty.call(props.props, 'event:bindings')) {
this.eventBindings = props.props['event:bindings'];
}
}
$destroy() { $destroy() {
destroy_component(this, 1); destroy_component(this, 1);

@ -34,6 +34,7 @@ export function createEventDispatcher<
return (type: string, detail?: any) => { return (type: string, detail?: any) => {
const callbacks = component.$$.callbacks[type]; const callbacks = component.$$.callbacks[type];
const eventBinding = component.eventBindings && Object.prototype.hasOwnProperty.call(component.eventBindings, type) ? component.eventBindings[type] : undefined;
if (callbacks) { if (callbacks) {
// TODO are there situations where events could be dispatched // TODO are there situations where events could be dispatched
@ -43,6 +44,12 @@ export function createEventDispatcher<
fn.call(component, event); fn.call(component, event);
}); });
} }
if (eventBinding) {
// in a server (non-DOM) environment?
const event = custom_event(type, detail);
eventBinding.call(component, event);
}
}; };
} }
@ -59,7 +66,7 @@ export function getAllContexts<T extends Map<any, any> = Map<any, any>>(): T {
} }
export function hasContext(key): boolean { export function hasContext(key): boolean {
return get_current_component().$$.context.has(key); return get_current_component().$$.context.has(key);
} }
// TODO figure out if we still want to support // TODO figure out if we still want to support

Loading…
Cancel
Save