woutdp / live_svelte

Svelte inside Phoenix LiveView with seamless end-to-end reactivity
https://hexdocs.pm/live_svelte
MIT License
1.2k stars 46 forks source link

Dealing with Promise #87

Closed absowoot closed 11 months ago

absowoot commented 11 months ago

I'm working on a component that is using an autocomplete library. I've tried a few libraries (Svelecte, Svelte Select Svelte Simple Autocomplete) and they all use promises to populate the dropdowns, but I'm looking to use an internal phoenix function.

I was able to accomplish this using Svelte Select by watching for keydown but I wonder if there would be a way to utilize the built-in async functions in these libraries with live_svelte?

woutdp commented 11 months ago

Could you send a bit of code of what you have and maybe some pseudocode of what you're trying to accomplish?

absowoot commented 11 months ago

Thanks for following up. Figures -- as I was writing out the issue I was able to figure it out.

For anyone who stumbles upon this in the future, I was able to use Svelecte and live_svelete by doing:

<script>
    import Svelecte from "svelecte/src/Svelecte.svelte";

    export let live;
    export let suggestions;

    const fetch = (query) => {
        return new Promise((resolve) => {
            live.pushEvent("get-suggestions", { search: query }, () => resolve(suggestions));
        });
    };
</script>

<Svelecte valueAsObject {fetch} />
accountex-apps commented 11 months ago

Thanks for posting your solution.