Giter Club home page Giter Club logo

Comments (7)

hutchgrant avatar hutchgrant commented on May 20, 2024 1

Yea that's just the example, you don't have to use webpack chunks. It does however assume the babel dynamic import plugin will be used in the implementation. But that's optional, for this optional feature. It's not a dependency for the module.

from lit-redux-router.

thescientist13 avatar thescientist13 commented on May 20, 2024

@hutchgrant
Curious, does this assume a hard dependency on webpack?

const AboutRoute = {
  load: () => import(/* webpackChunkName: "aboutPage" */ '../pages/home/about'),
  loading: 'eve-loading'
};
const DocsRoute = {
  load: () => import(/* webpackChunkName: "docsPage" */ '../pages/home/docs'),
  loading: 'eve-loading'
};

from lit-redux-router.

thescientist13 avatar thescientist13 commented on May 20, 2024

oh I see, this is just for your example usage, and not needed for the underlying implementation?

from lit-redux-router.

fernandopasik avatar fernandopasik commented on May 20, 2024

Thanks for opening this issue @hutchgrant, it will be a great addition!

from lit-redux-router.

fernandopasik avatar fernandopasik commented on May 20, 2024

This problem intersects with another use case that is if you have to query for data or any other async process before being able to display a route. (angular ui-router had an interface for this for example)

calling an api using a promise

<lit-route 
    path="/docs" 
    component="docs-page" 
    .resolve="${() => fetch('mydomain.com/api')}"
></lit-route>

dynamic import is a promise as well

<lit-route 
    path="/docs" 
    component="docs-page" 
    .resolve="${() => import(/* webpackChunkName: "aboutPage" */ '../pages/home/about')}"
></lit-route>

As you suggested we need a component for loading status

<lit-route 
    path="/docs" 
    component="docs-page" 
    .resolve="${() => import(/* webpackChunkName: "aboutPage" */ '../pages/home/about')}"
    loading="my-loading"
></lit-route>

And potentially add onSuccess and onError events for redirects or tracking or whatever

<lit-route 
    path="/docs" 
    component="docs-page" 
    .resolve="${() => import(/* webpackChunkName: "aboutPage" */ '../pages/home/about')}"
    loading="my-loading"
    @success="${result => console.log(result)}"
    @error="${error => console.log(error)}"
></lit-route>

from lit-redux-router.

fernandopasik avatar fernandopasik commented on May 20, 2024

I suggest the following implementation with a few changes in the stateChanged lifecycle and the render function

public stateChanged(state: State): void {
  this.active = isRouteActive(state, this.path);
  this.params = getRouteParams(state, this.path);

  if (this.active && this.resolve) {
    this.isResolving = true;
    this.resolve()
      .then(() => {
        this.isResolving = false;
        // here potentially fire custom success event
      })
      .catch(() => {
        this.isResolving = false;
        // here potentially fire custom error event
      });
  }
}
public render(): TemplateResult {
  if (!this.active) {
    return html``;
  }

  if (this.resolve && this.isResolving) {
    return !this.loading ? html`` : this.getTemplate(this.loading);
  }

  if (!this.component) {
    return html`<slot></slot>`;
  }

  return this.getTemplate(this.component, this.params);
}

from lit-redux-router.

hutchgrant avatar hutchgrant commented on May 20, 2024

That looks good to me, though I still have to test it. That's way better looking logic than my hacky method.

Also the success and error events should be optional as well.

from lit-redux-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.