Giter Club home page Giter Club logo

Comments (4)

ItalyPaleAle avatar ItalyPaleAle commented on August 10, 2024

Hi @notramo I am not sure I understand the issue. What loading screen?

from svelte-spa-router.

jh12z avatar jh12z commented on August 10, 2024

I'm also looking for the same functionality @notramo describes (correct me if I'm wrong). My component is expecting some data to be loaded and passed via props and that way I can avoid UI loading glitches easily.

It'd be nice if the options.props of the route accepts functions (async too) and waits until all the functions are resolved.
Also it'd be nice for the function to receive the same detail as the events.

Something like:

<!-- App.svelte -->
<script>
  import Router from 'svelte-spa-router';
  import wrap from "svelte-spa-router/wrap.js";
  import Loading from './Loading.svelte';
  import Product from './Product.svelte';

  async function sleep(time) {
    return await new Promise(res => setTimeout(res, time));
  }

  function logEvent(event) {
    console.log(event.type, event.detail);
  }

  let routes = {
    "/product/:id": wrap({
      component: Product,
      loadingComponent: Loading,
      conditions: [
        async (detail) => {
          console.log("condition1", detail);
          await sleep(1000); // fake delay
          return true;
        }
      ],
      props: {
        product: async (detail) => {
          console.log("Loading product", detail);
          await sleep(1000); // fake delay
          return {
            name: `Product ${detail?.params?.id}`
          }
        }
      }
    })
  }
</script>
<Router {routes}
  on:conditionsFailed={logEvent}
  on:routeLoading={logEvent}
  on:routeLoaded={logEvent}
  />

<!-- Loading.svelte -->
<div>Loading ...</div>

<!-- Product.svelte -->
<script>
  export let product = {};
</script>
<div>Name: {product.name}</div>

from svelte-spa-router.

jh12z avatar jh12z commented on August 10, 2024

I implemented the functionality in 5 lines but then I found there is a discussion already in #283. I'll review the other PR.

from svelte-spa-router.

notramo avatar notramo commented on August 10, 2024

Hi @notramo I am not sure I understand the issue. What loading screen?

The one that is currently shown while the asyncComponent is loading.

The solution proposed by @jh12z seems OK, but I'm not sure about how it handles changing route properties.
E.g. if I visit /items/1 then change the URL to /items/2, does it reload the item from the API?

from svelte-spa-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.