Code Monkey home page Code Monkey logo

Comments (2)

Mortaro avatar Mortaro commented on May 18, 2024

So far the two possible solutions me and @GuiDevloper came to are:

Explicit referral:

// referral in any link sets it to the context as is
<a href="/books/1" referral={book}>
// referral can be destructured anywhere from the context until the next anchor click replaces it
async initiate({ referral }) {
  this.book = referral || await this.getBookFromServer()
}

pro: this is very explicit
con: the code is very similar to setting an event that saves to the context and might become verbose

Implicit state

// state in a link forwards it to the next (first or all to be decided) match for the route on click
<a href="/books/1" state={{book}}>
// the component automatically receives the state before prepare is invoked
async initiate() {
  this.book ||= await this.getBookFromServer()
}

pro: you don't have to think to much about it, it is an nearly free optimization
con: it could cause problems because you are unaware of this dependency, so far Nullstack has no implicit behavior outside of the context

from nullstack.

Mortaro avatar Mortaro commented on May 18, 2024

The same behavior can be achieved in a more granular way using the already implemented features of the context with the advantage of being able to cache the full list and implementing advanced features like stale while revalidate.

Ill close this issue with an example using cache + revalidation in a explicit manner that implicit state would prevent implementing:

class Books extends Nullstack {

  prepare({ project, page, cachedBooks }) {
    page.title = `Books - ${project.name}`;
    this.books = cachedBooks;
  }

  static async getBooks({ database }) {
    return await database.collection('books').find().toArray();
  }

  async initiate(context) {
    this.books = await this.getBooks();
    context.cachedBooks = data.books;
  }

}
class Book extends Nullstack {
  
  static async getBookBySlug({ database, slug }) {
    return await database.collection('books').findOne({slug});
  }

  prepare({ params, cachedBooks }) {
    this.book = cachedBooks?.find(({ slug }) => slug === params.slug);
  }

  async initiate({ params, project, page }) {
    this.book = await this.getBookBySlug({ slug: params.slug });
  }

}

from nullstack.

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.