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

Best way to access global data initialized via ajax call? #535

Closed
Boscop opened this issue Jul 23, 2019 · 3 comments
Closed

Best way to access global data initialized via ajax call? #535

Boscop opened this issue Jul 23, 2019 · 3 comments
Labels

Comments

@Boscop
Copy link

Boscop commented Jul 23, 2019

What is the best way to access global data initialized via ajax call?
E.g. the app wants to load localization strings from the backend lazily, depending on which language's localization strings should be fetched. Every component of the app that renders strings needs to access the localization hashmap in order to be able to render its content. This should be done without any Msg roundtrip! Ideally, it should be just as convenient as accessing a global lazy_static hashmap.
How can it be done?
The requirements are:

  1. The localization strings are stored in a lazily initialized hashmap, it has to be initialized with the data returned from an ajax call to the backend
  2. Every component needs to be able to access this data without a Msg handling roundtrip, ideally just like accessing a global lazy_static var.

What are the different ways that it can be done in a convenient way?

@jstarry
Copy link
Member

jstarry commented Jul 24, 2019

Hmm good question.

If all of your components need it, sounds like you will want to kick off the request right away. Then you could have your root component show a loading screen until the fetch finishes. Once it's finished, how about you populate a mutable static hashmap which all components can access? https://doc.rust-lang.org/reference/items/static-items.html#mutable-statics

@Boscop
Copy link
Author

Boscop commented Sep 13, 2019

@jstarry But what's the best non-unsafe and non-blocking way? Using a global lazy_static RwLock?

@jstarry
Copy link
Member

jstarry commented Mar 8, 2020

I think this is more of a state question, let's continue this discussion in #576

@jstarry jstarry closed this as completed Mar 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants