From da4979fdfb98f6cb450323a8e6cd8a488c188ee3 Mon Sep 17 00:00:00 2001 From: Wout De Puysseleir Date: Sun, 26 Feb 2023 12:18:39 -0800 Subject: [PATCH] Update hooks.js --- assets/js/hooks.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/assets/js/hooks.js b/assets/js/hooks.js index 964c0ea..13289f1 100644 --- a/assets/js/hooks.js +++ b/assets/js/hooks.js @@ -13,6 +13,13 @@ function parsedProps(el) { return props ? JSON.parse(props) : {} } +function extraProps(ref) { + return { + pushEvent: (event, data, callback) => ref.pushEvent(event, data, callback), + goto: href => liveSocket.pushHistoryPatch(href, 'push', ref.el) + } +} + const SvelteComponent = { mounted() { const componentName = this.el.getAttribute('data-name') @@ -22,34 +29,18 @@ const SvelteComponent = { const Component = components[componentName] if (!Component) { - throw new Error(`Unable to find ${componentName} component. Did you forget to import it into hooks.js?`) - } - - const pushEvent = (event, data, callback) => { - this.pushEvent(event, data, callback) - } - - const goto = href => { - liveSocket.pushHistoryPatch(href, 'push', this.el) + throw new Error(`Unable to find ${componentName} component.`) } this._instance = new Component({ target: this.el, - props: {...parsedProps(this.el), pushEvent, goto}, + props: {...parsedProps(this.el), ...extraProps(this)}, hydrate: true }) }, updated() { - const pushEvent = (event, data, callback) => { - this.pushEvent(event, data, callback) - } - - const goto = href => { - liveSocket.pushHistoryPatch(href, 'push', this.el) - } - - this._instance.$$set({...parsedProps(this.el), pushEvent, goto}) + this._instance.$$set({...parsedProps(this.el), ...extraProps(this)}) }, destroyed() {