Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle Hooks: beforeSelect #749

Open
rbayley opened this issue Jul 25, 2021 · 2 comments
Open

Lifecycle Hooks: beforeSelect #749

rbayley opened this issue Jul 25, 2021 · 2 comments
Labels
question The issue is about question

Comments

@rbayley
Copy link

rbayley commented Jul 25, 2021

I am trying to load model with data when empty

I thought the beforeSelect lifecycle hook could be a good place to run the axios request but this lifecycle should ONLY return a collection and not a Promise.
So, is it a good idea to approach the problem this way?
If so, is it possible to have an alternative beforeSelect which returns a Promise?

Thanks!

@cuebit cuebit added the question The issue is about question label Aug 5, 2021
@cuebit
Copy link
Member

cuebit commented Aug 5, 2021

Probably not a good idea. All hooks such as beforeSelect are synchronous and should be used to handle read/write logic for data going in or out of the store.

You could use modules to manage entity-related state to determine whether logic needs to be executed in parallel such as requests to endpoints. Think of it as a layer of abstraction between components and vuex orm.

@coatesap
Copy link

coatesap commented Nov 3, 2021

I'm interested in achieving something similar - lazy loading results from an API on first access. Having done some quick experimentation, the following approach does seem to work...

static beforeSelect (records) {
  if (!fetched) {
    fetched = true
    goQueryApi().then(results => {
      this.insert({
        data: results
      })
    })
  }

  return records
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question The issue is about question
Projects
None yet
Development

No branches or pull requests

3 participants