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

Pass state prop as options.state before calling handlers #392

Closed
MrSwitch opened this issue Jan 22, 2025 · 2 comments
Closed

Pass state prop as options.state before calling handlers #392

MrSwitch opened this issue Jan 22, 2025 · 2 comments

Comments

@MrSwitch
Copy link
Member

options.state is only available on the root request, as dare traverses the tree and calls nested joined models the state is no longer there. Therefore the only reliable way has been to return the root requests initial options, i.e. dareInstance.options- the second parameter... see below

const myModel = {
    get(options, dareInstance) {
       const {state} = dareInstance.options;
       // use the state value to modify the request
    }
};
dare.use({models: {myModel}});

It's actually pretty rare to use dareInstance for anything other than retrieving the state object made in a request. And because it's not always obvious when it should occur to the layman, let's put it on each options<{state}>

So therefore we can reliably do...

const myModel = {
    get(options) {
       const {state} = options;
       // use the state value to modify the request
    }
};
const dare = new Dare({models: {myModel}});
@MrSwitch MrSwitch added the Feat label Jan 22, 2025
@MrSwitch MrSwitch self-assigned this Jan 22, 2025
5app-Machine added a commit that referenced this issue Jan 23, 2025
# [0.93.0](v0.92.1...v0.93.0) (2025-01-23)

### Features

* **state:** attach state to each tree node, fixes [#392](#392) ([#393](#393)) ([5221086](5221086))
@5app-Machine
Copy link
Contributor

🎉 This issue has been resolved in version 0.93.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@5app-Machine
Copy link
Contributor

🎉 This issue has been resolved in version 0.93.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants