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

Create global Alpine.$persist to enable persisting in Alpine.store #2191

Merged
merged 5 commits into from Oct 19, 2021

Conversation

MalcolmKnott
Copy link
Contributor

@MalcolmKnott MalcolmKnott commented Oct 7, 2021

Until now it has been impossible to persist values in an Alpine.store

This PR extracts the $persist magic to a globally accessible Alpine.$persist to enable use cases such as:

Alpine.store('darkMode', {
    on: Alpine.$persist(true)
})

The $persist magic is still registered, so this is a non-breaking change for exisiting $persist users.

We've included a test, as well as an index.html that makes it easy to play with the feature. Feel free to remove the index.html before merge. We also fixed a small bug that made rerunning the persist.spec.js in the same browser session not work.

@DanielCoulbourne says hi.

@MalcolmKnott MalcolmKnott marked this pull request as ready for review October 7, 2021 00:30
@DanielCoulbourne
Copy link

This right here is a grade A pull request! Only a FOOL would neglect this stunning contribution 💯

@SimoTod
Copy link
Collaborator

SimoTod commented Oct 7, 2021

Great addition! if you want to reset the index.html you can do git checkout master -- index.html.
It would be good to update the docs as well, it's in package/docs/src/en/plugins/persist.md

@calebporzio
Copy link
Collaborator

Thanks @MalcolmKnott! Good call @SimoTod - I agree - just do some cleanup on the PR (index.html and any stray extra lines) - then add docs to the Markdown file and we'll be good to goooooo

@MalcolmKnott MalcolmKnott force-pushed the add-global-persist branch 2 times, most recently from 48193c4 to 7f41ec0 Compare October 13, 2021 22:19
@MalcolmKnott
Copy link
Contributor Author

@calebporzio Should be ready for review now :)

<a name="using-alpine-persist-global"></a>
## Using the Alpine.$persist global

`Alpine.$persist` is exposed golobally so it can be used outside of `x-data` contexts. This is useful to persist data from other sources such as `Alpine.store`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo here @MalcolmKnott:

Suggested change
`Alpine.$persist` is exposed golobally so it can be used outside of `x-data` contexts. This is useful to persist data from other sources such as `Alpine.store`.
`Alpine.$persist` is exposed globally so it can be used outside of `x-data` contexts. This is useful to persist data from other sources such as `Alpine.store`.

@calebporzio
Copy link
Collaborator

Thanks @MalcolmKnott! This will be much appreciated by the community! Great work!

@calebporzio calebporzio merged commit 5c5d1a0 into alpinejs:main Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants